思路:
获取查询关键字
查询数据库,获得查询结果
将查询结果返回模板文件
forms.py
from flask_wtf import FlaskForm
from wtforms import StringField,SubmitField
from wtforms.validators import DataRequired
class SearchForm(FlaskForm):
keyword=StringField('keyword',validators=[DataRequired()])
submit=SubmitField('搜索')
case_manage.html
<div class="page-header" style="padding-top:60px;">
<div id="navbar" class="navbar-collapse collapse">
{{form.csrf_token}}
<form class="navbar-form" method="GET" action="{{url_for('gogotest_cases.search')}}">
{{form.keyword(placeholder='请输入搜索关键词',class_='form-control')}}
{{form.submit(class_='btn btn-success')}}
</form>
</div>
</div>
app.py - 视图函数
@app.route('/search')
def search():
form = SearchForm()
keyword = request.args.get('keyword')
search_result=TestCase.query.filter_by(creater=keyword).all()
if search_result:
return render_template('auto_test_case.html', cases=search_result, form=form)
else:
return 'Not Found'
注意:
获取GET数据:
from flask import request
...
request.args.get('keyword')
获取POST数据:
if form . validate on subm ():
keyword=form.keyword.data
参考文档: