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

Crispy forms inline radio button doesn't work for me

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

不确定我做错了什么但这只显示普通单选按钮而不是内联

看起来像是一个简单的疏忽:在您的 __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))
            )