class RatingForm(forms.Form):
def __init__(self, *args, lista_de_productores, **kwargs):
super(forms.Form, self).__init__(*args, **kwargs)
for p in lista_de_productores:
CHOICES = (('1', '1',), ('2', '2',), ('3', '3',) , ('4', '4',) , ('5', '5',))
self.fields[str(p)] = forms.ChoiceField(required=True, widget=forms.RadioSelect(), choices=CHOICES)
helper = FormHelper()
helper.layout = Layout(
InlineRadios(str(p))
)
不确定我在做什么错,但这只会显示普通的单选按钮,而不是嵌入式
答案 0 :(得分:2)
看起来像一个简单的疏忽:在您的__init__
方法中,您需要设置self.helper
属性,而不是创建一个名为helper
的变量。这应该为您工作:
class RatingForm(forms.Form):
def __init__(self, *args, lista_de_productores, **kwargs):
super(forms.Form, self).__init__(*args, **kwargs)
for p in lista_de_productores:
CHOICES = (('1', '1',), ('2', '2',), ('3', '3',) , ('4', '4',) , ('5', '5',))
self.fields[str(p)] = forms.ChoiceField(required=True, widget=forms.RadioSelect, choices=CHOICES)
# set the self.helper property:
self.helper = FormHelper()
self.helper.layout = Layout(
InlineRadios(str(p))
)