Reputation: 115
My HTML code:
<form method="GET" action="/">
<input type="text" name="query">
<button type="submit">Search</button>
</form>
My python code:
@app.route('/alumni',methods=['POST','GET'])
def alumni():
if request.method == 'GET':
query = request.form['query']
return render_template('alumni.html',query=query)
When I'm loading page it is giving error, I know that this error is because when we are loading page for first time we will have no query, if I use method=POST
to pass queries then my problem will be solved but POST
will not show the query, I want query to be shown, what to do?
Upvotes: 0
Views: 95
Reputation: 59164
Since request.form
is a dictionary-like object, you can use the .get
method to provide a default value:
query = request.form.get('query', '')
This will use the query
argument when available, or an empty string when it is not.
Update: Since you are using the GET method you should probably be using args
:
query = request.args.get('query', '')
Upvotes: 1