类型对象'RadioSelect'没有属性'渲染器'

时间:2017-11-17 17:05:38

标签: django django-forms

我正在尝试进行radioselect水平对齐,我收到以下错误消息:类型对象'RadioSelect'没有属性'渲染器'

我做错了什么?

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

1 个答案:

答案 0 :(得分:3)

您尝试继承forms.RadioSelect.renderer而不是forms.RadioSelectforms.RadioSelect未实例化,因此它没有该属性。所以你得到了属性错误。

from django.utils.safestring import mark_safe

class HorizontalRadioRenderer(forms.RadioSelect):
   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
           ),
         )