我正在尝试进行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),
)
答案 0 :(得分:3)
您尝试继承forms.RadioSelect.renderer
而不是forms.RadioSelect
。 forms.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
),
)