David Yang
David Yang

Reputation: 93

How to set a style class to a choice field in a Django form

I am trying to set a bootstrap-select class to my choice field. However, I am getting a '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'}),


            }

Upvotes: 5

Views: 8375

Answers (2)

Anton Shurashov
Anton Shurashov

Reputation: 1930

You set Meta.widgets, so you should set a widget instead of a field:

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

Upvotes: 0

d2718nis
d2718nis

Reputation: 1269

Try this:

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

Or just use forms.Select():

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

Upvotes: 4

Related Questions