Can't retrieve variable from url with Flask app after submitting search form

sabiv

I would like to present a new view after the user submits a search form. I made it as the same way as I do with my other views, but unfortunately this time doesn't happens anything, I can't retrieve the content from the app route. (So this question is not a duplicate of this question, the issue occurs only after submitting the form, it works perfectly in every other situation.) I write something into the form, submit it, then the url changes in the browser, but the view doesn't change anyway. I'm almost sure that it's because the ? and = in the search slug, but don't know how should I deal with them in the Python code.

Actually when I submit the form my browser redirects me to an url like this:

http://domain/.com/?search=content+from+textfield

And this is how I tried to catch the content from the search field and present a new view on the Flask's side:

@app.route('/?search=<url_content>', methods=['POST'])
def hello_url(url_content): 
return render_template("search-results.html", searchString = url_content])

I would really appreciate if somebody could show me the right way, basically I just wanna retrieve the value of <url_content> inside the hello_url function after the search button tapped.

Here's my html:

<form>
 <div class="form-group">
    <input type="search" class="form-control text-center input-lg" id="inputSearch" name="search" placeholder="search">
    </div>
     <br>
<div class="text-center"><button type="submit" class="btn btn-primary btn-lg">Search!</button></div>
</form>
davidism

You're confusing url parameters, which are captured with <variable>, with query parameters, which are accessed in request.args. Remove the query parameter from your route definition and access it in the view.

from flask import request

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/search')
def search():
    search = request.args.get('search')  # will be None if form wasn't submitted
    # do something with search
    return render_template('search.html', search=search)

index.html:

<form action="{{ url_for('search') }}">
    <input name="search"/>
    <input type="submit" value="Search"/>
</form>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Variable isn't changing after form submitting

How can I insert a variable into the resulting url parameter string after submitting my GET form?

Retrieve Mechanical Soup results after submitting a form

Can't clear the angular-json-schema-form after submitting

405 when submitting form to Flask app

How to retrieve ORM object in scope while submitting a form in flask

How to take a variable from inside a html form that wasn't inputted by the user and output it to flask app?

Can't retrieve $_POST value from URL

PHP - $_POST variable is not set after form submitting

nodemon app crashed after submitting form data

Go to the url of the selection after submitting the form

Django: can't retrieve variable from my view after AJAX call

Can't get the input from my HTML form in my python flask app

Form doesn't validate after submitting in moodle

Flask Form not submitting

"Method Not Allowed" error in Flask after submitting form data

internal server error after submitting a form flask,sqlite3

Can I retrieve data from NSUserDefaults after app termination?

In my spring login form url getting changed after submitting the form

File_get_html url variable from input search FORM

can I get text from html and put it into variable in php without submitting a form?

How can I decrement a field value from firestore after submitting a form successfully?

How can i get data or pass a data from one page to another after submitting a form in reactjs

I can't retrieve an access token from Spotify for my app

I can't retrieve the xcdatamodeld data from the app on the widgetKit extension

Submitting a form doesn't work after time, only after refreshing

WPF Can't retrieve WebP image from url?

Why can't I retrieve a URL from an XPath query?

stop form from submitting (tried e.preventDefault() after $.post but it stops form from submitting

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive