选择字段 WTForm 烧瓶下拉列表类型错误: 'NoneType'对象不可迭代



我编辑了这个问题,面临着不同的问题

File "/home/amar/tts/FLASKAPP/voicetune/venv/lib/python3.7/site-packages/wtforms/fields/core.py", line 471, in pre_validate
for v, _ in self.choices:
TypeError: 'NoneType' object is not iterable

form.py 文件

language = SelectField(u'Language', coerce=str)

在 routes.py 文件中

def dashboard():
form = DashboardForm()
form.language.choices = [(lang.language_name,lang.language_label) for lang in Languages.query.all()]
return render_template('dashboard.html', title='Dashboard', form=form)

仪表板.html文件

{{ form.language(class="form-control btn btn-info dropdown-toggle") }}

这是我在以前的项目中使用的一种使用表单选择多个字段的方法。

Class add(Form)
conceptList = SelectMultipleField('conceptList', coerce=int)
# Dynamic adding a concept
def __init__(self, *args, **kwargs):
Form.__init__(self, *args, **kwargs)
# Get the concepts from concepts table
self.conceptList.choices = [(c.concept_id, c.concept_name) for c in Concepts.query.order_by('concept_name')]

在视图文件上,我使用此方法从表单中检索数据。

for concept_id in form.conceptList.data:
concept = Concepts.query.get(concept_id)
new_tool.concepts_tools.append(concept)

它不在下拉菜单中,但您可能会发现它很有用。不同之处在于选择是在表单中声明的。我不确定这一点,但我认为您需要将 selectField 从字符串更改为 int,并在选择中引用语言的 id。像这样:

language = SelectField(u'Language', coerce=int)
def dashboard():
form = DashboardForm()
form.language.choices = [(lang.language_id,lang.language_name,lang.language_label) for lang in Languages.query.all()] 
return render_template('dashboard.html', title='Dashboard', form=form) 

最新更新