Issue
I have a base.html wherein the navigation bar has a search box.
<form class="form-inline my-2 my-lg-0" action={{ url_for('search') }} method="POST">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
<input class="form-control mr-sm-2" name="search" type="search" placeholder="Search Storage Request" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
Here is the view:
@app.route('/search', methods=['GET', 'POST'])
def search():
query = request.args['search']
req_search = Storage.query.filter_by(req_no=query)
return render_template('search.html', req_search=req_search)
But when I searched anything from the navigation bar search box. Here is the error I get:
query = request.args['search']
AttributeError: 'function' object has no attribute 'args'
Somehow I am not able to grab the search query in the view.
Solution
in general, search requests are performed with GET
method and you don't even need to mention method="GET"
since it's the default method. Also you don't need the csrf_token
in your form since it's a GET
request.`
so i suggest you those fixes in
your template
<form class="form-inline my-2 my-lg-0" action="{{ url_for('search') }}">
<input class="form-control mr-sm-2" name="search" type="search" placeholder="Search Storage Request" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
your view function
@app.route('/search') # 'GET' is the default method, you don't need to mention it explicitly
def search():
# query = request.args['search']
query = request.GET.get('search') # try this instead
req_search = Storage.query.filter_by(req_no=query)
return render_template('search.html', req_search=req_search)
Answered By - cizario
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.