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>
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.
Comments