AuthenticationForm 'data' keyword argument?

55 views
Skip to first unread message

james.pete...@gmail.com

unread,
Feb 14, 2019, 7:47:23 AM2/14/19
to Django users
Hi,

I have just been following the Django documention on forms:


And I was having an issue with validating the posted data in the view code. 

I came across this Stack Overflow post :


Where the solution to my problem was to use the data Keyword argument when creating a form from the POST'd data. Looking back on the docs I noticed that this wasn't specified. 

So my view function now looks like this:

def login(request):
    if request.method == 'POST':
        form = LoginForm(data=request.POST) #<- This is where I had to use the data kwarg
        if form.is_valid():
            return HttpResponseRedirect('#')
        return render(request, 'scoreboard/login.html', {'form': form})
    else:
        return render(request, 'scoreboard/login.html', {'form': LoginForm()})



data kwarg.png



Tim Graham

unread,
Feb 14, 2019, 9:49:55 AM2/14/19
to Django users
From the docs for AuthenticationForm: "Takes request as its first positional argument, which is stored on the form instance for use by sub-classes."

https://docs.djangoproject.com/en/dev/topics/auth/default/#django.contrib.auth.forms.AuthenticationForm

james.pete...@gmail.com

unread,
Feb 14, 2019, 8:34:37 PM2/14/19
to Django users
So I should be using:

form = AuthenticatioForm(request)

It seems odd that the signature for various form aren't the same, at least in respect to *args. **kwags I can understand being different. Regardless I wanted to highlight a gotcha that wasn't immediately obvious. 

Also, I received no error from using:

form = AuthenticatioForm(request.POST)

If it's wrong shouldn't django let me know?
Reply all
Reply to author
Forward
0 new messages