user1050619
user1050619

Reputation: 20856

Django choicefield choices not displaying

Im using a choicefield and setting 2 values - 'students', 'teachers', but for some reason when the form displays it only shows 'teachers' and not 'students'.

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

Upvotes: 1

Views: 1753

Answers (1)

Peter Razkevych
Peter Razkevych

Reputation: 49

Please look here You add to your choices only values without keys. Code might look like this:

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

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

Upvotes: 1

Related Questions