Align radio buttons horizontally in django forms Align radio buttons horizontally in django forms django django

Align radio buttons horizontally in django forms


Thats the behavior of the RadioField. If you want it rendered horizontally, create a horizontal renderer, like something as follows:

from django.utils.safestring import mark_safeclass HorizontalRadioRenderer(forms.RadioSelect.renderer):  def render(self):    return mark_safe(u'\n'.join([u'%s\n' % w for w in self]))class ApprovalForm(forms.Form):    approval = forms.ChoiceField(choices=APPROVAL_CHOICES,                 initial=0,                 widget=forms.RadioSelect(renderer=HorizontalRadioRenderer),                                 )


Another way out is to changing the style of ul->li list to display:inline-block. You can do something like that

 <style> ul#youelementId li{  display: inline-block;  }</style>

hope this would help next reader.


I've come up with an alternative solution. If you are using bootstrap to render your forms, you can add the .form-check-inline class to the input and the field will display horizontally. Listed below is code that shows what I'm describing. I hope this helps someone from reinventing the wheel. Thanks for reading. Take care and have a good day.

                feature_type = forms.MultipleChoiceField(                required=False,                ...                widget=forms.CheckboxSelectMultiple(attrs={'class': 'form-check-inline'})                )

This image shows a set of radio buttons displayed horizontally.