我是flask新手,不知道如何在发布失败后保留字段数据。
谢谢你的帮助^_^。
举个例子。
views.py:
@app.route('/', methods=['GET', 'POST', ])
def index():
form = MyForm()
if request.method == 'GET':
return render_template('index.html', form=form)
elif request.method == 'POST':
if form.validate_on_submit():
# blabla...
return redirect('/')
else: # validate false
# how to keep field data in new page?
return render_template('index.html', form=form) # it failed
因为我实现了自己的html表单字段,所以没有成功,为了解决这个问题,我应该写这样的模板。
index. html:
<form ...>
{{ form.fieldname(class_='form-control', placeholder='hint') }}
</form>
与其从wtforms.Form继承你的Form类, 不如从flask_wtf.FlaskForm继承.
例如,替换这个
from wtforms import Form
class RegistrationForm(Form):
#fields...
有了这个
from flask_wtf import FlaskForm
class RegistrationForm(FlaskForm):
#fields...