[Django]-Radio buttons in django Forms

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, 
    )
👤dting

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.

Leave a comment