Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Authenticate returns None

I have the following code snippet:

user = User(username='[email protected]',email='[email protected]')
user.set_password('pass')
user.save()
u = authenticate(username='[email protected]', password='pass') #this always returns None!!!

The problem is, u is always None. I've followed code samples on other stack overflow posts and have narrowed it down to the above lines.

Any ideas as to what might be happening?

like image 967
David Glass Avatar asked Sep 06 '13 17:09

David Glass


People also ask

What does authenticate return in Django?

Authenticating Users It checks the credentials against the authentication backend and returns User objects if they are valid. If they are not valid for a backend or they have no permissions, Django will return “none.”

Does Django have authentication?

Django provides an authentication and authorization ("permission") system, built on top of the session framework discussed in the previous tutorial, that allows you to verify user credentials and define what actions each user is allowed to perform.


1 Answers

Put something like this in your settings

#Authentication backends
AUTHENTICATION_BACKENDS = (
        'django.contrib.auth.backends.ModelBackend',
    )

or if you are using userena for your accounts

#Authentication backends
AUTHENTICATION_BACKENDS = (
    'userena.backends.UserenaAuthenticationBackend',
    'guardian.backends.ObjectPermissionBackend',
    'django.contrib.auth.backends.ModelBackend',
)
like image 175
Ramast Avatar answered Nov 22 '22 15:11

Ramast