Recently I developed a web application using flask, which has a search page and a results page. The search page has multiple forms. Currently, these forms have been successfully processed in the routing function of the search page, and the results are stored in a list type. In the variable, I want to pass this variable to another page, that is, the results page, and display the results. Is there any way to pass parameters between routes?
@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
Use a global variable
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")
高洛峰2017-05-18 10:51:28
Requests directly correspond to results.
Why do you need to make another request to get the result after one request is completed?
淡淡烟草味2017-05-18 10:51:28
Use the redirect function
return redirect(url_for('fresult')), and you can add parameters to the function.
怪我咯2017-05-18 10:51:28
@app.route('/search', methods=['get', 'post']) #这是搜索页面
def fsearch():
....
if request.method == 'POST':
results = multiselect(request) #这是处理表单的函数,reslults为list类型变量
....
return return render_template("result.html", results=results)
return render_template("new.html")
phpcn_u15822017-05-18 10:51:28
Why do you have to use post? You can refer to my implementation
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