问题
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