form_valid and form_invalid never called in django form


February 2019


954 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


Ты путаешь django.forms.Formс django.views.generic.edit.FormView.

Оба form_validи form_invalidметоды принадлежат Джанго класса на основе общего вида FormView .

FormКласс, который AuthenticationForm расширяет , имеет is_validметод вместо этого.