django模型选择字段不会呈现为单选按钮

时间:2014-11-03 18:26:29

标签: django django-models django-forms

我正在尝试显示一个广播组而不是默认下拉列表。我的表格如下:

class ConditionForm(forms.ModelForm):

    state = forms.ChoiceField(choices=STATE_TYPES, widget=forms.RadioSelect())

    class Meta:
        model = Customer
        fields = ('state',)

我也试过这个: class ConditionForm(forms.ModelForm):

    class Meta:
        model = Customer
        fields = ('state',)
        widgets = {
        'state': forms.RadioSelect(),
    }

我的模型看起来像这样:

STATE_TYPES = (
    (0, 'Type 2'),
    (1, 'Type 1'),
)
class Customer(models.Model):
    state = models.IntegerField(choices=STATE_TYPES, default=0, null=True)
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)

我的模板如下:

{{ form.state }}

这会输出一个下拉菜单,而不是4个单选按钮。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

我认为我的css中存在问题并使用django小部件调整。我从django widget调整中删除了添加css并将其添加到表单中并且它有效。

像这样:

state = forms.ChoiceField(choices=STATE_TYPES, widget=forms.RadioSelect(attrs={'class':'radio_1', 'name': 'name2'}))

答案 1 :(得分:0)

您应该只输出字段,而不是遍历其属性。只是:

{{ form.state }}
相关问题