Align radio buttons horizontally in django forms

前端 未结 7 1781
借酒劲吻你
借酒劲吻你 2021-02-08 01:50

HI

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

feature_type  = forms.TypedChoiceField(choices =         


        
7条回答
  •  星月不相逢
    2021-02-08 02:45

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

提交回复
热议问题