1

Django version 1.10.7

I'm getting TypeError: authenticate() takes exactly 0 arguments (3 given)

Importing the authenticate() like this:
from django.contrib.auth import authenticate, login

Calling authenticate() like this :
authenticate(request, username=request.POST['username'] ,password=request.POST['password'])

Rahul Verma
  • 2,946
  • 14
  • 27

3 Answers3

2

Set the authenticate to a variable and don't pass in request, such as:

auth = authenticate(username=request.POST['username'] ,password=request.POST['password'])

And then use login to get the user logged in:

login(request, auth)

Don't forget to import login

csling
  • 326
  • 1
  • 8
  • https://docs.djangoproject.com/en/1.11/topics/auth/default/#how-to-log-a-user-in : In the given example the authenticate is used like `user = authenticate(request, username=username, password=password)` – Rahul Verma Aug 28 '17 at 21:49
  • Wish I could accept all 3 answers here. Anyways I have upvoted all of these. Thanks for a quick answer. – Rahul Verma Aug 28 '17 at 21:59
2

In 1.10 authenticate does not take positional arguments (docs) call it as

authenticate(username=request.POST['username'], password=request.POST['password'])
chicocvenancio
  • 629
  • 5
  • 14
  • https://docs.djangoproject.com/en/1.11/topics/auth/default/#how-to-log-a-user-in : In the given example the authenticate is used like `user = authenticate(request, username=username, password=password)` – Rahul Verma Aug 28 '17 at 21:47
  • @batMan True, but pay attention to versions. In 1.11 Django added an optional request argument, but 1.10 did not have it. – chicocvenancio Aug 28 '17 at 21:53
  • Ok So that's optional only in 1.11. Makes sense. I just tried and it worked. Wish I could accept all 3 answers here. Anyways I have upvoted all of these. Thanks for a quick answer. – Rahul Verma Aug 28 '17 at 21:59
2

Use authenticate without the request, only takes username and password authenticate(username=XXX,password=XXX). See the documentation https://docs.djangoproject.com/en/1.11/_modules/django/contrib/auth/#authenticate

aspo
  • 374
  • 2
  • 9
  • https://docs.djangoproject.com/en/1.11/topics/auth/default/#how-to-log-a-user-in : In the given example the authenticate is used like `user = authenticate(request, username=username, password=password)` – Rahul Verma Aug 28 '17 at 21:49
  • You are using Django version 1.10.7, the optional was added in version 1.11. Read the notes. – aspo Aug 28 '17 at 21:53
  • Wish I could accept all 3 answers here. Anyways I have upvoted all of these. Thanks for a quick answer. – Rahul Verma Aug 28 '17 at 21:59