Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render ChoiceField with radio buttons

I've constructed this form from a model.

class Configure_template(forms.Form):
    subject_type = forms.ChoiceField(choices=Subject_type.objects.all())

And I want to render this using radio buttons but I have problems with the for in the html,

Thanks for any suggestion.

like image 834
Antonio Villavicencio Garzón Avatar asked Oct 13 '13 15:10

Antonio Villavicencio Garzón


2 Answers

Use a RadioSelect widget on your form field:

subject_type = forms.ChoiceField(choices=Subject_type.objects.all(),
    widget=forms.RadioSelect)
like image 129
Timmy O'Mahony Avatar answered Nov 18 '22 01:11

Timmy O'Mahony


If you want to show radiobuttons with choices from database use cannot use ChoiceField.

You have to use a ModelChoiceField.

subject_type = forms.ModelChoiceField(widget=forms.RadioSelect, queryset=Subject_type.objects.all(), label='')
like image 23
K Neeraj Lal Avatar answered Nov 18 '22 01:11

K Neeraj Lal