提交后清除有效的表单 [英] Clear valid form after it is submitted
问题描述
@ mod.route('/',methods = ['GET','POST '))
$ b $ flash(gettext(u'Thanks for the registration。'))
def home():
form = NewRegistration()
$ b $ if form.validate_on_submit():
#save in db
$ b return render_template(users / registration.html,form = form)
return render_template
问题在于,您始终使用传入的任何数据来呈现表单,即使数据验证和处理。另外,浏览器存储最后一次请求的状态,所以如果刷新页面,浏览器将重新提交表单。
处理成功表单请求,重定向到页面以获得新鲜状态。
@ app.route('/ register',methods = [ ')
def register():
form = RegistrationForm()
$ b $如果form.validate_on_submit():
#有效的形式
#然后重定向到结束形式
返回重定向(url_for('register'))
#初始化get或form没有验证
返回render_template('register.html',form = form)
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屋!