Clear valid form after it is submitted

后端 未结 2 879
一个人的身影
一个人的身影 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:55

    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)
    

提交回复
热议问题