[Django]-Using AuthenticationForm in Django

64👍

Try:

form = AuthenticationForm(data=request.POST)

26👍

After a few hours spent finding “Why the hack there are no validation error?!” I run into this page: http://www.janosgyerik.com/django-authenticationform-gotchas/

AuthenticationForm ‘s first argument is not data! at all:

def __init__(self, request=None, *args, **kwargs):

So thats why you have to pass req.POST to data or pass something else in the first argument. It was already stated in one of the answers here to use:

AuthenticationForm(data=req.POST)

You can also use one of these:

AuthenticationForm(req,req.POST)
AuthenticationForm(None,req.POST)

2👍

The code of is_valid function:

return self.is_bound and not bool(self.errors)


>>> form.errors
{'__all__': [u'Please enter a correct username and password. Note that both fields are case-sensitive.']}

If you see the code of method clean of AuthenticationForm

def clean(self):
    username = self.cleaned_data.get('username')
    password = self.cleaned_data.get('password')

    if username and password:
        self.user_cache = authenticate(username=username, password=password)
        if self.user_cache is None:
            raise forms.ValidationError(_("Please enter a correct username and password. Note that both fields are case-sensitive."))
        elif not self.user_cache.is_active:
            raise forms.ValidationError(_("This account is inactive."))
    self.check_for_test_cookie()
    return self.cleaned_data

“The problem” is that does not exits a user with this username and passowrd

👤Goin

Leave a comment