我有一个模型生成的django表单。
class Manager(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
gender = models.CharField(max_length=50)
birth_date = models.DateField()
phone_number = models.CharField(max_length=10)
region_of_residence = models.CharField(max_length=50)
city_of_residence = models.CharField(max_length=50)
common_of_redidence = models.CharField(max_length=50)
address_of_residence = models.CharField(max_length=50)
zip_of_residence = models.CharField(max_length=50)
username = models.CharField(max_length=20)
email = models.CharField(max_length=200)
password = models.CharField(max_length=100)
def __str__(self):
return self.username
我创建了一种表单来覆盖某些字段,并以这种方式添加其他字段:
class ManagerForm(forms.ModelForm):
gender = forms.ChoiceField(choices=GENDER_CHOICES)
password = forms.CharField(widget=forms.PasswordInput())
verify_password = forms.CharField(widget=forms.PasswordInput())
class Meta:
model = Manager
fields = ('first_name', 'last_name', 'gender', 'birth_date',
'phone_number', 'region_of_residence', 'city_of_residence',
'common_of_redidence', 'address_of_residence',
'zip_of_residence', 'username', 'email', 'password',)
def clean(self):
cleaned_data = super(ManagerForm, self).clean()
password = cleaned_data.get("password")
verify_password = cleaned_data.get("verify_password")
if password != verify_password:
raise forms.ValidationError(
"password and verify_password does not match"
)
我已将选择放入外部文件中,它们是:
GENDER_CHOICES = [
_('Non specificato'),
_('Uomo'),
_('Donna')
]
现在我想用一个简单的for语句呈现表单
{% for field in form %}{% render_field field class="form-control" %}.
但是我收到以下错误:
ValueError: too many values to unpack (expected 2)
如何正确渲染这些字段?
答案 0 :(得分:2)
CHOICES必须是由db值和显示值组成的2元组的列表。例如:
GENDER_CHOICES = [
("N", _('Non specificato')),
("U", _('Uomo')),
("D", _('Donna'))
]
答案 1 :(得分:0)
为什么不使用以下方式呈现表单:
{{ form }}