Django选择字段选择不显示

时间:2016-10-07 03:27:11

标签: django

我使用了一个选择字段并设置了2个值 - '学生''教师', 但由于某种原因,当表单显示时,它只会显示教师'而不是学生'。

class SignUpShortForm(SignUpForm):
    role = forms.ChoiceField(
        choices=[],
        widget=forms.Select(attrs={'class':'form-control'}),
        label='I am a...',
    )
    self.fields['role'].choices = [('Teacher', 'Teacher2')]

1 个答案:

答案 0 :(得分:1)

请查看here您只能在没有键的情况下为您的选择添加值。代码可能如下所示:

CHOICES = (
    ('students', 'Students'),
    ('teachers', 'Teachers'),
)

class SignUpShortForm(SignUpForm):
    role = forms.ChoiceField(
        choices=CHOICES,
        widget=forms.Select(attrs={'class':'form-control'}),
        label='I am a...',
    )
相关问题