WTForms “too many values to unpack” with SelectFie

2019-06-16 05:03发布

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?

2条回答
Rolldiameter
3楼-- · 2019-06-16 05:38

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>'
查看更多
登录 后发表回答