Django Authenticate returns No

I have the following code snippet:

user = User(username='h@h.com',email='h@h.com') user.set_password('pass') user.save() u = authenticate(username='h@h.com', password='pass') #this always returns None!!! 

The problem is that u is always None. I followed the code examples in other columns and narrowed them down to the lines above.

Any ideas on what might happen?

+8
authentication django nonetype
source share
5 answers

Put something like this in your settings

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

or if you use userena for your accounts

 #Authentication backends AUTHENTICATION_BACKENDS = ( 'userena.backends.UserenaAuthenticationBackend', 'guardian.backends.ObjectPermissionBackend', 'django.contrib.auth.backends.ModelBackend', ) 
+9
source share

Interestingly, check_password returns True in the following:

 eml = "4@a.com" pw = "pass" uname = 'w2' user = User.objects.create_user(uname,eml,pw) user.save() log.debug("Password check passes?") log.debug(user.check_password(pw)) # Logs True!!! user = authenticate(username=uname, password=pw) 
+1
source share

In settings.py add

 AUTH_USER_MODEL = your custom user class 

For example, if the django application name is the office, and the user class is the account, then

 AUTH_USER_MODEL = 'office.Account' 
+1
source share

Why don't you create such a user:

 user = User.objects.create_user( username="whatever", email="whatever@some.com", password="password") user = authenticate( username="whatever",password="password") 
0
source share

Changed to the following. Another glory

 email = "2@a.com" password = "pass" user = User.objects.create_user(email,email,password) u = authenticate(username=email, password=password) 
0
source share

All Articles