Bootstrap导航栏搜索将搜索查询发送到Flask视图 [英] Bootstrap navbar search to send search query to Flask view
问题描述
我有一个base.html,其中导航栏有一个搜索框.
< form class =" form-inline my-2 my-lg-0"action = {{url_for('search')}} method ="POST"><输入类型=隐藏的".name ="csrf_token";value ="{{csrf_token()}}"//<输入类别="form-control mr-sm-2".名称=搜索"type =搜索"placeholder =搜索存储请求"aria-label =搜索">< button class ="btn btn-outline-success my-2 my-sm-0";type =提交">搜索</button></form>
这是视图:
@ app.route('/search',Methods = ['GET','POST'])def search():查询= request.args ['搜索']req_search = Storage.query.filter_by(req_no = query)返回render_template('search.html',req_search = req_search)
但是当我从导航栏搜索框中搜索任何内容时.这是我得到的错误:
query = request.args ['search']AttributeError:函数"对象没有属性"args"
以某种方式,我无法在视图中获取搜索查询.
通常,搜索请求是使用 GET
方法执行的,您甚至不需要提及 method ='GET"
,因为它是默认方法.另外,您的表单中也不需要 csrf_token
,因为它是 GET
请求.
所以我建议您使用这些修补程序
您的模板
< form class ="form-inline my-2 my-lg-0"action =" {{url_for('search')}}'><输入类别="form-control mr-sm-2".名称=搜索"type =搜索"placeholder =搜索存储请求"aria-label =搜索">< button class ="btn btn-outline-success my-2 my-sm-0";type =提交">搜索</button></form>
您的查看功能
@ app.route('/search')#'GET'是默认方法,您无需明确提及def search():#查询= request.args ['搜索']query = request.GET.get('search')#试试这个req_search = Storage.query.filter_by(req_no = query)返回render_template('search.html',req_search = req_search)
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.
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)
这篇关于Bootstrap导航栏搜索将搜索查询发送到Flask视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!