Clear valid form after it is submitted
Asked Answered
N

4

31

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)
Niemeyer answered 11/8, 2015 at 15:0 Comment(0)
P
49

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)
Pren answered 11/8, 2015 at 15:16 Comment(0)
R
8

davidism answer is correct.

But once I had to reload a form with only a few fields that had to be resetted. So, I did this, maybe it's not the cleanest way but it worked for me:

form = MyForm()

    if form.validate_on_submit():
        # save all my data...
        myvar1 = form.field1.data
        myvar2 = form.field2.data
        # etc...

    # at first GET and at every reload, this is  what gets executed:
    form.field1.data = "" # this is the field that must be empty at reload
    form.field2.data = someobject # this is a field that must be filled with some value that I know

    return render_template('mypage.html', form=form)
Resh answered 30/10, 2019 at 13:9 Comment(1)
Hello, if i am using html forms, how do i pass on the previous value after form submission fail (say because of recaptcha not valid)Hoye
A
3

You can clear a form by passing formdata=None

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

    if form.validate_on_submit():
        #save in db

        ######### Recreate form with no data #######
        form = NewRegistration(formdata=None)

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

    return render_template("users/registration.html", form=form)
Automatic answered 18/10, 2022 at 20:16 Comment(1)
imo this answer is better than @PrenAltruism
C
0

you can also return new form object using render_template if form does not validates you can also pass message

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

if form.validate_on_submit():
    #save in db

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

return render_template("users/registration.html", form=form)
Constellate answered 25/10, 2022 at 8:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.