![](/img/trans.png)
[英]Flask WTForm with SelectField does not work with if form.validate_on_submit
[英]How to manually create a form and then validate it in Flask wtform?
如何手動填充我的表單然后驗證它?
class TestForm(Form):
name = StringField('name', validators=[InputRequired()])
age = IntegerField('age', validators=[InputRequired()])
我嘗試使用任意數據構建表單並驗證:
data = {'name': 'bob', 'age': 33}
tform = TestForm(name=data['name'], age=data['age'])
if tform.validate():
print 'success!'
else:
print tform.errors
結果打印出錯誤:
{'age': [u'This field is required.']}
這很奇怪,因為它沒有抱怨name
,即使我將它們都提供給了我的TestForm
。 我究竟做錯了什么?
嘗試這樣做
data = {'name': 'bob', 'age': 33}
tform = TestForm()
tform.name.data = data['name']
tform.age.data = data['age']
if tform.validate():
print 'success!'
else:
print tform.errors
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.