Using AuthenticationForm in Django
Asked Answered
A

3

36

I'm attempting to use the AuthenticationForm form with django and finding out that I can't seem to get the form to validate. I widdled it down to a simple test(assuming it's correct) that doesn't seem to work. Can anyone see a problem here?

>>> from django.contrib.auth.forms import AuthenticationForm
>>> POST = { 'username': 'test', 'password': 'me', }
>>> form = AuthenticationForm(POST)
>>> form.is_valid()
False

Is there a real reason it won't validate? Am I using it incorrectly? I've basically modeled it after django's own login view.

Archoplasm answered 7/12, 2011 at 19:22 Comment(0)
D
65

Try:

form = AuthenticationForm(data=request.POST)
Duster answered 7/12, 2011 at 21:5 Comment(4)
Thanks. Not sure how I missed that in django's default view. All my other forms I just throw POST into the instance, without using a kwarg. Any idea what's different here?Archoplasm
If memory serves, the AuthenticationForm is slightly different than the others. There are a few inconsistencies here and there in Django. Typically I instantiate forms using: form = MyForm(request.POST or None)Duster
Needs to be AuthenticationForm(None, request.POST)Denunciation
@Denunciation Keping Stack Overflow answers updated would be a full-time job.Duster
E
27

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)
Eskimo answered 1/2, 2014 at 22:33 Comment(1)
Good call on the 'data='.Donia
F
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

Frankel answered 7/12, 2011 at 19:36 Comment(3)
Where are you seeing this is_valid? django.forms.BaseForm? What version of django are you running? I'm on 1.3.1 here. If this is using the same backend as the django admin, I have tried with a valid user/pass...Archoplasm
In trunk: code.djangoproject.com/browser/django/trunk/django/forms/… or in the Django 1.3.1 code.djangoproject.com/browser/django/tags/releases/1.3.1/…Frankel
form.errors was empty. The answer above this one has fixed the problem. at hand. Thanks for this information, though.Archoplasm

© 2022 - 2024 — McMap. All rights reserved.