选择字段WTForm烧瓶下拉列表TypeError:“ NoneType”对象不可迭代

时间:2019-09-19 09:52:53

标签: python flask flask-sqlalchemy flask-wtforms

我已经编辑了此问题,面临另一个问题

  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)

在route.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)

dashboard.html文件

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

1 个答案:

答案 0 :(得分:0)

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

let collectionView: UICollectionView = {
   let layout = UICollectionViewFlowLayout()
   let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
   collectionView.delegate = self // You cannot use
   collectionView.dataSource = self // You cannot use
   return collectionView 
}()

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

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')]

它不在下拉菜单中,但是您可能会发现它很有用。不同之处在于选项以表格形式声明。我对此不确定,但是我认为您需要将selectField从string更改为int,并在选择中同时引用语言的ID。像这样:

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