form_valid and form_invalid never called in django form


February 2019


966 time

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?


1 answers


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.