44
You already have the user when you save the form, so you don’t need to call authenticate
since you already provide the backend when calling login()
:
user = form.save()
login(request, user, backend='django.contrib.auth.backends.ModelBackend')
3
Came here looking for this error. Our stack is django-oscar + wagtail. It turns out we removed oscar.apps.customer.auth_backends.EmailBackend
from our AUTHENTICATION_BACKENDS
. Putting it back solved the issue.
- [Django]-Copy file from one model to another
- [Django]-Django rest framework – how do you flatten nested data?
- [Django]-What is the most efficient way to store a list in the Django models?
2
I believe it’s because you havent hashed the password. This worked for me. Try:
user = userform.save()
user.set_password(user.password)
user.save()
- [Django]-Chaining multiple filter() in Django, is this a bug?
- [Django]-Mongoengine creation_time attribute in Document
- [Django]-Having trouble with user.is_authenticated in django template
1
The UserCreationForm() provides for both password and the password_confirmation fields.
Authentication fails in this case because you are trying to get “password” which does not exists, therefore returning user as None.
If you print form.cleaned_data, you get a dictionary similar to this
{'username': 'myuser', 'password1': 'pass1234', 'password2': 'pass1234'}
Changing the raw_pass line should fix the issue:
raw_pass = form.cleaned_data.get('password1')
- [Django]-How do I convert datetime.timedelta to minutes, hours in Python?
- [Django]-Phpmyadmin logs out after 1440 secs
- [Django]-How can I get tox and poetry to work together to support testing multiple versions of a Python dependency?
1
Cause of Returning None While logging with created user from registration form , in DB it is checking specific user with encrypted password ,but we are saving password in text from that is why if you give even correct username and password ,it is failing
Add below model backends in setting.py file
AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend',)
or pass backend to login function itself
login(request, username,password, backend='django.contrib.auth.backends.ModelBackend')
import make_password function and pass password to it which is comming from registration form then it will save password into Db in encrypted form
from django.contrib.auth.hashers import make_password
raw_pass = form.cleaned_data.get('password')
raw_pass = make_password(form.cleaned_data.get('password'))
Django=2.2.4
- [Django]-What's the purpose of Django setting ‘SECRET_KEY’?
- [Django]-Add data to ModelForm object before saving
- [Django]-How to see details of Django errors with Gunicorn?
1
Ran into the same issue (using Django 2.2).
To fix I added AUTH_USER_MODEL = 'myapp.MyUser'
to settings.py above MIDDLEWARE
.
From the 2.2 Docs on substituting a custom User model:
" Django allows you to override the default user model by providing a value for the AUTH_USER_MODEL setting that references a custom model: AUTH_USER_MODEL = 'myapp.MyUser'
. This dotted pair describes the name of the Django app (which must be in your INSTALLED_APPS), and the name of the Django model that you wish to use as your user model "
- [Django]-Can I make STATICFILES_DIR same as STATIC_ROOT in Django 1.3?
- [Django]-How to use visual studio code to debug django
- [Django]-Update django database to reflect changes in existing models
0
I was running into the same problem because I had customized authentication and was passing wrong arguments in authenticate()
You did not share your custom user model, but it could be that you are using email as the USERNAME_FIELD
.
class User(AbstractUser):
email = models.EmailField(unique=True)
USERNAME_FIELD = 'email'
So in the relevant view, you will need to pass email (and not username) as an argument to authenticate()
if form.is_valid():
form.save()
email = form.cleaned_data['email']
password = form.cleaned_data['password1']
user = authenticate(request, email=email, password=password)
login(request, user)
...
- [Django]-Limit foreign key choices in select in an inline form in admin
- [Django]-Django – Website Home Page
- [Django]-Django: TypeError: 'tuple' object is not callable