2013-04-03 23 views
0

在我的應用程序,我需要的用戶失望list.I以爲我會創建一個對這篇如下Django的形式顯示一個下拉列表導致類型錯誤

class CIForm(forms.Form): 
    intervaloption = forms.ChoiceField(choices = [x for x in range(1,10)],label='Days taken') 

然而,從下拉菜單中選擇一個整數值,當我試圖顯示此使用as_p()在Django殼,扔一個TypeError

In [29]: f= CIForm() 

In [30]: f 
Out[30]: <__main__.CIForm object at 0xaa93eec> 

In [31]: print f.as_p() 

ERROR: An unexpected error occurred while tokenizing input 
The following traceback may be corrupted or invalid 
The error message is: ('EOF in multi-line statement', (41, 0)) 

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
... 
TypeError: 'int' object is not iterable 

我不能做什麼,我做錯了..can請你幫忙嗎?

回答

2

ChoiceField文檔:

選擇

可迭代的2元組(例如,列表或元組),以使用作爲 選擇用於該字段。該參數接受與模型字段的 選擇參數相同的格式。有關更多詳細信息,請參閱模型字段參考 選項文檔。

像這樣的東西應該工作:

class CIForm(forms.Form): 
    intervaloption = forms.ChoiceField(choices = [(str(x), str(x)) for x in range(1,10)], label='Days taken') 
+0

非常感謝..我的錯不讀小心的文檔 – damon