提交后清除有效表格 [英] Clear valid form after it is submitted

查看:30
本文介绍了提交后清除有效表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在表单验证后重置表单.目前表单提交并生效后,仍会显示之前的数据.基本上,我希望表单在所有字段都干净的情况下恢复到原始状态.这样做的正确做法是什么?

I want to reset the form after it validates. Currently the form will still show the previous data after it is submitted and valid. Basically, I want the form to go back to the original state with all fields clean. What is the correct to do this?

@mod.route('/', methods=['GET', 'POST'])
def home():
    form = NewRegistration()

    if form.validate_on_submit():
        #save in db

        flash(gettext(u'Thanks for the registration.'))

    return render_template("users/registration.html", form=form)

推荐答案

问题是您总是使用传入的任何数据呈现表单,即使该数据经过验证和处理.另外,浏览器会保存上次请求的状态,所以如果此时刷新页面,浏览器会重新提交表单.

The issue is that you're always rendering the form with whatever data was passed in, even if that data validated and was handled. In addition, the browser stores the state of the last request, so if you refresh the page at this point the browser will re-submit the form.

处理成功的表单请求后,重定向到页面以获取新状态.

After handling a successful form request, redirect to the page to get a fresh state.

@app.route('/register', methods=['GET', 'POST'])
def register():
    form = RegistrationForm()

    if form.validate_on_submit():
        # do stuff with valid form
        # then redirect to "end" the form
        return redirect(url_for('register'))

    # initial get or form didn't validate
    return render_template('register.html', form=form)

这篇关于提交后清除有效表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆