【问题标题】:how to implement a nested route in flask that each do different things如何在烧瓶中实现一个嵌套路由,每个路由都做不同的事情
【发布时间】:2019-03-31 02:19:01
【问题描述】:
@app.route('/sera/<mount_type>')
@app.route('/sera', methods=['POST'])
def return_pages():
    if request.method == 'POST':
        usin = request.form.get('serval')
        global mount_type
        mount_type = usin 
    #this section runs independend of the search box
    if mount_type == 'acongagua':
        return render_template('result.html',aa=acongagua.names, ac=acongagua.location, ad=acongagua.metre_height, ae=acongagua.feet_height)
    elif mount_type == 'adams':
        return render_template('result.html',aa=adams.names, ac=adams.location, ad=adams.metre_height, ae=adams.feet_height)
    else:
        return 'YOU HAVE ENTERED AN INCORRECT VALUE'

if __name__ == '__main__':
    app.run(debug=True, use_reloader=True)

这是我尝试运行的 python 烧瓶代码,第一个 if 语句应该在填写表单时运行,并且应该将其值传递给 seconf if 语句

【问题讨论】:

标签: python-3.x flask


【解决方案1】:

您可以使用redirect 重定向请求,并将值作为URL 参数传递:

from flask import redirect

@app.route('/sera/<mount_type>')
@app.route('/sera', methods=['POST'])
def return_pages():
    if request.method == 'POST':
        usin = request.form.get('serval')
        return redirect(url_for('return_pages', mount_type=usin))  # <--
    ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-02
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多