繁体   English   中英

Flask WTForms validate_on_submit 不起作用

[英]Flask WTForms validate_on_submit not working

我是 Web 开发的新手,我正在使用 Flask 创建一个房地产价格预测网站。

我有一个不工作的 function validate_on_submit。 它没有显示任何错误,表单已提交,只是没有验证。 当点击提交表单时,它需要go 到下一页。 这是代码:

@app.route('/route1', methods=['POST', 'GET'])
def predict():
    form = Form_1()

    # These errors, submitted and validated just for some context, not in the actual code

    print(form.errors) # Returns {}

    if form.submit():
       print("submitted") # Returns "submitted"

    if form.validate():
       print("validated") # Page shows error 'NoneType' object is not iterable

    # more code

    if form.validate_on_submit():
        print("validated on submit") # This is not working

        # more code

        return redirect(url_for('page_x'))
    return render_template('page_x.html', title='Page X', form=form)

这是 HTML:

<div class="content" align="center">
    <div class="content-section">
        <form method = "POST" action="">
            {{ form.hidden_tag() }}
            <table style="width:15%">
                <tr>
                    <td>{{ form.select_field.label() }}</td>
                    <td>:</td>
                    <td>{{ form.select_field() }}</td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                    <td>{{ form.submit() }}</td>
                </tr>
            </table>
        </form>
    </div>
</div>

这很奇怪,因为我有另一个类似的代码有效:

@app.route('/route2', methods=['POST', 'GET'])
def add_data():
    form = Form_2()
    if form.validate_on_submit():

        # more code

        return redirect(url_for('page_y')
    return render_template('page_y.html', title='Page Y', form=form)

HTML:

<div class="content" align="center">
    <h1>Help Us Improve by Uploading a New Dataset</h1>
    <div class="content-section">
        <form method="POST" action="" enctype="multipart/form-data">
            {{ form.hidden_tag() }}
            {{ form.add_file() }} {{ form.submit() }}
        </form>
    </div>
</div>

我不确定出了什么问题。 任何帮助,将不胜感激。 谢谢你。

我发现了问题。 我使用的是 SelectField 表单。 我放置了一个coerce arguments 并且它可以工作。

帮助我的文章: 不是动态 Select 字段 WTFORMS 的有效选择

暂无
暂无

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

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