Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align radio buttons horizontally in django forms

HI

I want to align the radio buttons horizontally. By default django forms displays in vertical format.

feature_type  = forms.TypedChoiceField(choices = formfields.FeatureType, widget = forms.RadioSelect)

Is there any special parameter that we can pass for radio buttons alignment?

Thanks in advance

like image 280
vkrams Avatar asked May 09 '11 10:05

vkrams


People also ask

How do I align a radio button horizontally?

To make a horizontal radio button set, add the data-type="horizontal" to the fieldset . The framework will float the labels so they sit side-by-side on a line, hide the radio button icons and only round the left and right edges of the group.

How do I align a horizontal radio group?

Yes, there is a way. Drag a radio list widget to your screen, go to the Properties tab and select 'Orientation' -> Horizontal.


1 Answers

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_safe

class 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),
                                 )
like image 166
lprsd Avatar answered Sep 22 '22 07:09

lprsd