form_valid and form_invalid never called in django form

Refresh

February 2019

Views

966 time

1
from django.contrib.auth.forms import AuthenticationForm

class LoginForm(AuthenticationForm):


    def clean(self):
        logger.debug("LoginForm: clean")
        cleaned_data = self.cleaned_data
        mail = cleaned_data.get("username")
        pwd = cleaned_data.get("password")
        user = authenticate(username=mail, password=pwd)

        ...

        cleaned_data = super(LoginForm, self).clean()
        return cleaned_data


    def form_invalid(self, form):
        logger.debug("LoginForm: form_invalid")
        return super(LoginForm, self).form_invalid(form)

    def form_valid(self, form):
        logger.debug("LoginForm: form_valid")
        return super(LoginForm, self).form_valid(form)

The methods form_valid and form_invalid are never called. However clean is always called (I see the message "LoginForm: clean"). What's wrong?

rom

1 answers

4

You're confusing django.forms.Form with django.views.generic.edit.FormView.

Both form_valid and form_invalid methods belong to the Django's class-based generic view FormView.

The Form class, which AuthenticationForm extends, has a is_valid method instead.