脆皮表单内联单选按钮对我不起作用

时间:2018-10-04 22:14:32

标签: django django-crispy-forms

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

不确定我在做什么错,但这只会显示普通的单选按钮,而不是嵌入式

1 个答案:

答案 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))
            )