最近用flask开发一个web应用,其中有一个搜索页面和结果页面,搜索页面有多个表单,目前在搜索页面的路由函数中已经成功处理这些表单,得到的结果存储在了一个list类型的变量里面,我想将这个变量传递到另一个页面也就是结果页面中,将结果显示出来,有什么路由之间传递参数的方法吗
@app.route('/search', methods=['get', 'post']) #这是搜索页面
def fsearch():
....
if request.method == 'POST':
results = multiselect(request) #这是处理表单的函数,reslults为list类型变量
...
return render_template("new.html")
@app.route('/result', methods=['get', 'post']) #这是结果页面
def fresult():
...
return render_template("result.html")
淡淡烟草味2017-05-18 10:51:28
用个全局变量
results = None
@app.route('/search', methods=['get', 'post']) #这是搜索页面
def fsearch():
....
if request.method == 'POST':
global results
results = multiselect(request) #这是处理表单的函数,reslults为list类型变量
...
return render_template("new.html")
@app.route('/result', methods=['get', 'post']) #这是结果页面
def fresult():
global results
print results
return render_template("result.html")
phpcn_u15822017-05-18 10:51:28
为什么一定要用post呢,可以参考我的实现
class SearchView(MethodView):
def get(self):
query_dict = request.data
page, number = self.page_info
keyword = query_dict.pop('keyword', None)
include = query_dict.pop('include', '0')
if keyword and len(keyword) >= 2:
fields = None
if include == '0':
fields = ['title', 'content']
elif include == '1':
fields = ['title']
elif include == '2':
fields = ['content']
results = Topic.query.msearch(
keyword, fields=fields).paginate(page, number, True)
data = {'title': 'Search', 'results': results, 'keyword': keyword}
return render_template('search/result.html', **data)
data = {'title': 'Search'}
return render_template('search/search.html', **data)
demo