Clear valid form after it is submitted

后端 未结 2 863
一个人的身影
一个人的身影 2020-11-30 10:14

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

2条回答
  •  情歌与酒
    2020-11-30 10:42

    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)
    

提交回复
热议问题