0
votes

I made a custom user form with fields username, password1 and pasword2, based on UserCreationForm.

I want to style it using bootstrap form-control in Meta widgets, but it doesn't work.

Is this possible?

EDIT

Although password1 and password2 are not in Meta fields, they are rendered.

The code:

class UserForm(UserCreationForm):
    class Meta:
        model = User
        fields = ['username']
        widgets = {
            'username': TextInput(attrs = {
                'class': 'form-control',
            }),
            'password1': PasswordInput(attrs = {
                'class': 'form-control',
            }),
            'password2': PasswordInput(attrs = {
                'class': 'form-control',
            }),                       
        }
1

1 Answers

0
votes

Solved it like this:

class UserForm(UserCreationForm):
    def __init__(self, *args, **kwargs):
        super(UserForm, self).__init__(*args, **kwargs)
        self.fields['username'].widget = TextInput(attrs = {'class': 'form-control',})
        self.fields['password1'].widget = PasswordInput(attrs = {'class': 'form-control',})
        self.fields['password2'].widget = PasswordInput(attrs = {'class': 'form-control',})
    class Meta:
        model = User
        fields = UserCreationForm.Meta.fields