Django 'AnonymousUser' object has no attribute '_meta'
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.
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')
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
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')