Align radio buttons horizontally in django forms

前端 未结 7 1888
借酒劲吻你
借酒劲吻你 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:39

    There is actually no need to override the widget, template or anything else in admin. At least since 2008 (see forms.css), the easiest way is to pass a class attribute inline: attrs={'class': 'inline'}

    In a custom form, it may look like:

    field = forms.ChoiceField(choices=(('1', 'one'), ('2', 'two')),
                              widget=forms.RadioSelect(attrs={'class': 'inline'}))
    

    … and it works the very same for checkboxes:

    field = forms.MultipleChoiceField(choices=(('1', 'one'), ('2', 'two')),
                                      widget=forms.CheckboxSelectMultiple(attrs={'class': 'inline'}))
    

    And it should be the very same for formfield overrides, either via ModelAdmin formfield_overrides or formfield_for_* functions.

提交回复
热议问题