使用SelectField,WTForms“解压”的数量太多了

时间:2011-06-20 21:44:05

标签: python forms wtforms

我正在使用WTForms而我正在尝试显示SelectField,但是我收到以下错误:

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

这是我的表格:

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

Test.statuses静态方法返回字符串列表。我做错了什么?

2 个答案:

答案 0 :(得分:18)

答案 1 :(得分:4)

没关系,它需要元组,而不是字符串:

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