浏览器标题切换
浏览器标题切换end

Flask中根据表单中下拉框的选择跳转指定页面

HomePage.html中存在一个表单:
注意:action不写的话,路由默认在 / 下方,则该路径下方的函数名可以随意取

<h4>Complete the Form</h4>

<form id="upload_form" method="POST" action="/upload_csv_file">
    <div class="form-group">
        <select class="custom-select" name="dropdown_box_model">
            <option selected>Select a model (Required)</option>
            <option value="1">Test 1</option>
            <option value="2">Test 2</option>
            <option value="3">Test 3</option>
        </select>
       /* or <button type="submit">Submit</button> */
    </div>
</form>

{#......#}

<input type="submit" form="upload_form" class="btn btn-block1 btn-lg btn-primary mt-4"
       value="View Results">

app.py 中的路由:

@app.route("/upload_csv_file", methods=['POST'])
def upload_csv_file():
    selected = request.form['dropdown_box_model']
    if selected == '1':
        return render_template("../test1.html")
    elif selected == '2':
        return render_template("../test2.html")

# Running the app
if __name__ == '__main__':
    app.run(debug=True)

Flask标准写法:

@app.route('/', methods=['POST'])
def jump_page():
    x = request.form['dropdown_box_model']
    if x == '1':
        return redirect(url_for('1'))
    elif x == '2':
        return redirect(url_for('2'))
    else:
        return 'Error'
posted @ 2023-03-08 18:34  抓水母的派大星  阅读(111)  评论(0编辑  收藏  举报