Peksio
Peksio

Reputation: 585

Django Forms and bootstrap - adding widgets works but not fully

I have problem with bootstrap in my django forms. In forms I have this code:

make = forms.ChoiceField(choices=make_choices)
make.widget.attrs.update({'class': 'form-control'})

And in tutorials I saw that it should be enough to render proper bootstrap choice field.

But in my case it looks like this: enter image description here Please notice:

I checkout out several tutorials but in all of them adding this "{'class': 'form-control'}" in attrs was enough. I was also experimenting with django crispy forms but they were also not fully correct. What am I missing? What I did wrong?

Upvotes: 1

Views: 1079

Answers (2)

nikola jole
nikola jole

Reputation: 7

UPDATE: 2023

'field_name': forms.Select( attrs={'class': 'form-control'})

If you using widget in Forms.py

class Name(forms.ModelForm):
class Meta:
    model = modelName
    fields = {'field1', 'field1'}
    widgets = {
        'field1': forms.Select( attrs={'class': 'form-control'})
    }

Upvotes: 0

cizario
cizario

Reputation: 4254

as of bootstrap 5 docs https://getbootstrap.com/docs/5.0/forms/select/#default you should use .form-select class to trigger the custom styles for <select> elements, so it should be

make.widget.attrs.update({'class': 'form-select'})

.form-control class is used for elements like <input> and <textarea>.

Upvotes: 4

Related Questions