custom backend causes this error: authenticate() takes exactly 0 arguments (2 given)

1,896 views
Skip to first unread message

Gloria

unread,
Nov 24, 2009, 11:40:00 AM11/24/09
to Django users
Hi all,

I am using the SettingsBackend sample code, from here:

http://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.authenticate

in Django 1.1.1, python 2.6, and I get this error:

TypeError at /login/
authenticate() takes exactly 0 arguments (2 given)

I've tried making SettingsBackend inherit from ModelBackend (which is
what I really want), but this does not change the behavior.

Thanks in advance,
Gloria

Tom Evans

unread,
Nov 24, 2009, 11:43:50 AM11/24/09
to django...@googlegroups.com
Sounds like you have overridden the correct authenticate() function with one that takes no arguments - hard to tell without seeing any code!

Do you have any methods, files etc called 'authenticate'?

Cheers

Tom

Gloria

unread,
Nov 24, 2009, 12:11:55 PM11/24/09
to Django users
Aye, there's the rub. My authenticate takes at least one parameter:

from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.auth.backends import ModelBackend

class AuthBackend(ModelBackend):
"""
Custom authentication: No password, because of SOAP
passthrough.
"""
def authenticate(self, username, password=None):
try:
user = User.objects.get(username=username)
if not user:
# Create a new user. Note that we can
set password
# to anything, because it won't be
checked; the password
# from settings.py will.
user = User(username=username,
password="default")
user.is_staff = False
user.is_superuser = False
user.save()

return user

except:
#except User.DoesNotExist:
# Create a new user. Note that we can set
password
# to anything, because it won't be checked;
the password
# from settings.py will.
user = User(username=username,
password="default")
user.is_staff = False
user.is_superuser = False
user.save()
return user


def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None

Tom Evans

unread,
Nov 24, 2009, 12:21:28 PM11/24/09
to django...@googlegroups.com
What does the traceback look like?

Cheers

Tom

Gloria

unread,
Nov 24, 2009, 12:28:21 PM11/24/09
to Django users
Environment:

Request Method: POST
Request URL: http://localhost:8000/login/
Django Version: 1.1.1
Python Version: 2.6.0
Installed Applications:
['attachments',
'avatar',
'soaplib',
'notification',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.flatpages',
'django.contrib.redirects',
'django.contrib.humanize',
'django.contrib.comments',
'django_extensions',
'ajax_validation',
'djangoratings',
'debug_toolbar',
'solango',
'voting',
'tagging',
'compress',
'opencrowd.django.apps.portal_layout',
'mailer',
'pg_logging.singleton_logger',
'pg.ip.misc',
'pg.ip.taxo',
'pg.ip.search',
'pg.ip.client',
'pg.ip.profile',
'pg.ip.community',
'pg.ip.activity',
'pg.ip.performance',
'pg.ip.comments']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',

'opencrowd.django.apps.portal_layout.middleware.PageLayoutFallbackMiddleware')


Traceback:
File "/usr/lib/python2.6/site-packages/Django-1.1.1-py2.6.egg/django/
core/handlers/base.py" in get_response
92. response = callback(request, *callback_args,
**callback_kwargs)
File "/mnt/XP_D/Opencrowd/PressGaney/pg_trunk/main/pg/ip/profile/
views.py" in local_login
82. user = authenticate(user_name)

Exception Type: TypeError at /login/
Exception Value: authenticate() takes exactly 0 arguments (1 given)

Thanks,
Gloria

Tom Evans

unread,
Nov 24, 2009, 12:40:29 PM11/24/09
to django...@googlegroups.com
On Tue, Nov 24, 2009 at 5:28 PM, Gloria <stra...@comcast.net> wrote:
<snip>
Traceback:
File "/usr/lib/python2.6/site-packages/Django-1.1.1-py2.6.egg/django/
core/handlers/base.py" in get_response
 92.                 response = callback(request, *callback_args,
**callback_kwargs)
File "/mnt/XP_D/Opencrowd/PressGaney/pg_trunk/main/pg/ip/profile/
views.py" in local_login
 82.   user = authenticate(user_name)

Exception Type: TypeError at /login/
Exception Value: authenticate() takes exactly 0 arguments (1 given)

Thanks,
Gloria

Hmm, I'm not sure you're doing this correctly. You shouldn't be trying to call the authenticate() method on your custom backend, you should be calling django.contrib.auth.authenticate, with whatever usernames/tokens/etc you have to provide. Django's auth system will iterate through its backends, as defined in settings.AUTHENTICATION_BACKENDS, calling each of them in turn until one of them returns something other than None.

See http://docs.djangoproject.com/en/dev/topics/auth/#writing-an-authentication-backend for more details.

I still think you are directly calling a function called authenticate() that is not django.contrib.auth.authenticate()..

Cheers

Tom

Gloria

unread,
Nov 24, 2009, 1:25:38 PM11/24/09
to Django users
After further examination, it looks like Django is completely ignoring
my setting:

AUTHENTICATION_BACKENDS =
('pg.ip.profile.auth_backend.AuthBackend','django.contrib.auth.backends.ModelBackend')

Argh...

Gloria

Gloria

unread,
Nov 24, 2009, 1:30:52 PM11/24/09
to Django users
The post before this one never made it. Strange.
I have no other custom authenticate() methods. I did try to call my
own directly, and this worked, but it's not the recommended way.
Is there some more debug info I can turn on, to see what authenticate
() methods Djongo is trying to invoke? It's appanrently ignoring mine,
and gettiong one from somewhere not obvious to me.
Thanks for your help,
Gloria

Gloria

unread,
Nov 24, 2009, 11:43:39 PM11/24/09
to Django users

Oh, geez you are NEVER going to believe what caused this error.

This causes it:

user = authenticate(user_name)

This makes it work:

user = authenticate(username = user_name)

The error is vague because authenticate() accepts **kwargs.

How did I find it, you may ask? I set a breakpoint using pdb, right
before the authenticate() call. I then ran runserver, and stepped into
the callback. I saw that it was indeed authenticating against each
model given in the settings file.

Argh! Finally. Now I can sleep.

Gloria
Reply all
Reply to author
Forward
0 new messages