125👍
✅
Look at setting the field’s widget
and choices
when writing the form class.
from django import forms
class PictureForm(forms.Form):
CHOICES = [
('1', 'Option 1'),
('2', 'Option 2'),
]
like = forms.ChoiceField(
widget=forms.RadioSelect,
choices=CHOICES,
)
- The default
widget
of ChoiceField is a drop down select. - The
choices
argument has the same format as the one of a model field.
7👍
Remember to remove all the external styling, and then add the code below:
CHOICES = [('M','Male'),('F','Female')]
Gender=forms.CharField(label='Gender', widget=forms.RadioSelect(choices=CHOICES))
The above code generates Radio buttons.
In my case, I was using a style.css
file which was forcing the radio button to render as a list.
- [Django]-How to access the local Django webserver from outside world
- [Django]-Check if an object exists
- [Django]-Is it better to use path() or url() in urls.py for django 2.0?
Source:stackexchange.com