wtforms 使用的一些问题总结

Q&A

Q1. 用IntegerField来限制form只能用数字 但是发现它只能接受1以上 不能接受0 如何设置

A1:添加代码validators=[InputRequired()]

Q2. 让 IntegerField 的输入框只允许输入数字,即选渲染后的输入框类型为 number

A2: 添加代码widget=NumberInput(), 引入 from wtforms.widgets.html5 import NumberInput,默认的 widget 为 TextInput(),见链接[1]

Q3. 如何在一个页面中使用两个Form

A3: 确保两个form中的字段不相同,因为渲染之后字段名即为 id,判断语句应如下,其他方法可参考[2]:

1
2
3
4
if form1.validate_on_sumite() and form1.submite1.data():
...
elif form2.validate_on_submit() and form2.submit2.date():
...

Q4. 添加 placeholder

A4: 添加代码 render_kw={'placeholder': "placeholder"}

参考

[1]wtforms-fields-core.py
[2]Multiple forms in a single page using flask and WTForms
[3]widgets
[4]fields