python - passing parameters between different routes in flask
PHP中文网
PHP中文网 2017-05-18 10:49:28
0
5
1278

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")
PHP中文网
PHP中文网

认证高级PHP讲师

reply all(5)
淡淡烟草味

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")
小葫芦

Requests directly correspond to results.
Why do you need to make another request to get the result after one request is completed?

淡淡烟草味

Use the redirect function
return redirect(url_for('fresult')), and you can add parameters to the function.

刘奇
@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_u1582

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template