Wtforms:如何使用具有动态选择值的select字段生成空值。

时间:2022-05-20 11:46:15

I'm using Flask with WTForms (doc) on Google App Engine. What is the best way to generate an field with an empty value for a select field?

我在谷歌应用引擎上使用Flask和WTForms (doc)。为select字段生成一个空值字段的最佳方法是什么?

form.group_id.choices = [(g.key().id(), g.name) for g in Group.all().order('name')]

Is there something like "blank=True" for the form field?

表单字段是否存在类似“blank=True”的内容?

myfield = wtf.SelectField()

2 个解决方案

#1


13  

Can you just prepend an empty pair to the list?

你能在列表的前面加上一对空的吗?

form.group_id.choices.insert(0, ('', ''))

#2


3  

If it's a QuerySelectField, you can add parameters like this:

如果是QuerySelectField,可以添加如下参数:

allow_blank=True, blank_text=u'-- please choose --'

#1


13  

Can you just prepend an empty pair to the list?

你能在列表的前面加上一对空的吗?

form.group_id.choices.insert(0, ('', ''))

#2


3  

If it's a QuerySelectField, you can add parameters like this:

如果是QuerySelectField,可以添加如下参数:

allow_blank=True, blank_text=u'-- please choose --'