【问题标题】:How to set a style class to a choice field in a Django form如何将样式类设置为 Django 表单中的选择字段
【发布时间】:2018-01-10 06:45:17
【问题描述】:

我正在尝试为我的选择字段设置一个引导选择类。但是,我收到“TypeError: init() got an unexpected keyword argument 'attrs'”。

class constraintListForm1(forms.Form):

    region = forms.ChoiceField(choices=REGION, required=True)   
    operator = forms.ChoiceField(choices=OPERATOR, required=True )

    class META:
        model = constraintListModel1  


        widgets = {
            'region': forms.ChoiceField(attrs={'class' : 'bootstrap-select'}),


            }

【问题讨论】:

标签: html django django-forms bootstrap-4 bootstrap-select


【解决方案1】:

试试这个:

widgets = {
    'region': forms.ChoiceField(
        widget=forms.Select(attrs={'class':'bootstrap-select'})
    ),
}

或者直接使用forms.Select():

widgets = {
    'region': forms.Select(attrs={'class':'bootstrap-select'}),
}

【讨论】:

  • David Yang,很高兴听到这个消息。如果回答对您有帮助,请标记为正确的。谢谢!
【解决方案2】:

你设置了Meta.widgets,所以你应该设置一个小部件而不是一个字段:

class Meta
    model = constraintListModel1  
    widgets = {
        'region': forms.Select(attrs={'class': 'bootstrap-select'}),
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    相关资源
    最近更新 更多