0

I'm trying to add a class to <input> on my form. Following the instructions of the first answer to this question, I added some code into my form's __init__ method. However I got the error displayed in the title.

forms.py

class SignupForm(UserCreationForm):
    email = forms.EmailField(max_length=200, help_text='Required')
    first_name = forms.CharField(max_length=30)
    last_name = forms.CharField(max_length=30)

    def __init__(self, *args, **kwargs):
        form = super(SignupForm, self).__init__(*args, **kwargs)
        for visible in form.visible_fields():
            visible.field.widget.attrs['class'] = 'form-control'

    class Meta:
        model = User
        fields = ('username', 'email', 'password1', 'password2', 'first_name', 'last_name',)

1 Answer 1

1

You get the error because the super() call returns None. Inside the __init__ method you can access the form's attributes with with self.

def __init__(self, *args, **kwargs):
    super(SignupForm, self).__init__(*args, **kwargs)
    for visible in self.visible_fields():
        ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.