WTForms “too many values to unpack” with SelectField

不想你离开。 提交于 2019-12-23 07:40:07

问题


I'm using WTForms and I'm trying to display a SelectField, but I get the following error:

>>> form.status()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python26\Lib\site-packages\wtforms\fields.py", line 136, in __call__
    return self.widget(self, **kwargs)
  File "C:\Python26\Lib\site-packages\wtforms\widgets.py", line 237, in __call__
    for val, label, selected in field.iter_choices():
  File "C:\Python26\Lib\site-packages\wtforms\fields.py", line 390, in iter_choices
    for value, label in self.choices:
ValueError: too many values to unpack

Here's my form:

class TestForm(Form):
    status = SelectField(u'Status', choices=Test.statuses())

The Test.statuses static method returns a list of strings. What am I doing wrong?


回答1:


It expects a list of (str, str) tuples, not a list of strings.




回答2:


Never mind, it needs tuples, not strings:

>>> form.status.choices = [(status, status) for status in Test.statuses()]
>>> form.status()
u'<select id="status" name="status"><option value="Status1">Status1</option></select>'


来源:https://stackoverflow.com/questions/6417935/wtforms-too-many-values-to-unpack-with-selectfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!