[flask]jina2处理表单--实现搜索功能

 

 思路:

获取查询关键字

查询数据库,获得查询结果

将查询结果返回模板文件 

 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

 

参考文档:

flask系列---搜索功能的实现与页面倒计时跳转(十五)

posted @ 2019-11-18 11:05  wangju003  阅读(617)  评论(0编辑  收藏  举报