最近用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