dea*_*ost 6 forms django range choicefield
from django import forms
class SignUpForm(forms.Form):
birth_day = forms.ChoiceField(choices=range(1,32))
Run Code Online (Sandbox Code Playgroud)
我在渲染时遇到"Caught TypeError:'int'对象不可迭代". https://docs.djangoproject.com/en/dev/ref/models/fields/#field-choices说,choices参数采用列表或元组之类的迭代.
http://docs.python.org/library/functions.html#range表示range()创建一个列表.
为什么我收到错误?
我尝试使用map()将列表转换为str,但收到了不同的错误.
Ign*_*ams 16
...表示choices参数采用诸如列表或元组之类的迭代.
不,它说它需要一个可重复的2元组.
2元组的可迭代(例如,列表或元组),用作此字段的选项.
birth_day = forms.ChoiceField(choices=((str(x), x) for x in range(1,32)))
Run Code Online (Sandbox Code Playgroud)