繁体   English   中英

使用GET方法提交带烧瓶wtforms的表单

[英]Submit form with flask wtforms with GET method

我有2条路线:

@app.route('/activity', methods=["GET"])
def addactivity():
    return render_template('activity.html')

在acitvity.html中,我有一个包含一些字段的表单,我想将此表单提交到另一条路线:

@app.route('/students', methods=["GET"])
def addstudents():
    return render_template('students.html')

因此,当我在acitvity.html中提交表单时,我将被重定向到

/ students?field1 = value1&field2 = value2等...

对于活动表单,我正在使用带有验证器的wtforms:DataRequired()或InputRequired()

问题是当我在addstudents路由中使用form.validate()时,即使表单已通过验证,也会将我重定向到students.html路由。我希望万一表单未经验证以显示activity.html中的错误那怎么可能?

我是不熟悉Flask的人,也许我的整个代码是错误的,但是概念是,如果验证了活动表单,我将被重定向到学生路线,如果没有,则错误将显示在活动路线中。此外,我需要提交学生表格中的表格与前一张表格的args路由。

初始化表单时,您需要传递request.args ,例如

@app.route('/students')
def addstudents():
    form = Form(request.args)

    if form.validate():
        return render_template('students.html')

    # Redirect to addactivity if form is invalid
    return redirect(url_for('addactivity'))

您将需要从flask导入request对象。

来源: https : //github.com/lepture/flask-wtf/issues/291

要在提交表单时重定向到addstudents ,请将属性action="{{ url_for('addstudents') }}"添加到activity.html<form>标记中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM