简体   繁体   中英

Flask get request using value

I have the following form:

<form action="{{ url_for('search') }}" method="get">
  <input type="text" name="searchbar" placeholder="Search">
</form>

In

@app.route("/search/<string:searchtext>")
def search(searchtext):
    ...

how would I use the value of searchbar to generate <string:searchtext> ? Is there anything I could put as *value* in {{ url_for('search', searchtext=*value*) }} ?

A form using the method get sends the parameters values in the URL, such as: http://example.com/search?searchbar=foo

In Flask you can access this value using:

from flask import request

@app.route('/search')
def login():
    searchbar = request.args.get('searchbar')

If you want to pass the value in the URL path ( /search/<string:searchtext> ), then you have to redirect the user (using Javascript or a a link).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM