Got bit by this one again, which I assume is going to be pretty common if you’re trying to use the AuthenticationForm as you would normally use a form.
When I use forms, I get into the habit of doing something like this:
if request.method == 'POST':
f = MyForm(request.POST)
if f.is_valid():
do_whatever()
The problem when you use the AuthenticationForm is that init‘s has the first parameter named request. So if you do something like this:
if request.method == 'POST':
f = AuthenticationForm(request.POST)
if f.is_valid():
do_whatever()
You’ll see that the form is not valid, but you won’t get any errors. The explanation is listed in the comments here: http://code.djangoproject.com/ticket/9803, but it’s painful to learn what went wrong.
The right way to use the AuthenticationForm
is do this:
if request.method == 'POST':
f = AuthenticationForm(data=request.POST)
if f.is_valid():
do_whatever()
How would I fix this? Instead of doing this:
class AuthenticationForm(forms.Form):
def __init__(self,request=None, *args, **kwargs):
I would make request be an optional named parameter, and check for it. That’s what the code is doing later on in the clean method anyway.