Home > database >  'AnonymousUser' object has no attribute '_meta' | Django Authentication
'AnonymousUser' object has no attribute '_meta' | Django Authentication

Time:11-06

Same authentication system on three different places in project i.e Authenticating user at login, registration, and password reset. At password reset it works fine all the time. At registrations sometime works and sometime doesn't and at login works on rare occasions. Also the error is same all the time.

ERROR

AttributeError at /userauth/user-activate/NA/avnpw3-de3afda5cfeae9690598ace91235106a/smqia40453665072/pW1QdEFRkm42txOZ
'AnonymousUser' object has no attribute '_meta'
Request Method: POST
Request URL:    http://127.0.0.1:8000/userauth/user-activate/NA/avnpw3-de3afda5cfeae9690598ace91235106a/smqia40453665072/pW1QdEFRkm42txOZ
Django Version: 3.2.7
Exception Type: AttributeError
Exception Value:    
'AnonymousUser' object has no attribute '_meta'
Exception Location: C:\Users\smqia\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\functional.py, line 247, in inner
Python Executable:  C:\Users\smqia\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.7
Python Path:    
['C:\\xampp\\htdocs\\Projects\\Barter',
 'C:\\Users\\smqia\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
 'C:\\Users\\smqia\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
 'C:\\Users\\smqia\\AppData\\Local\\Programs\\Python\\Python39\\lib',
 'C:\\Users\\smqia\\AppData\\Local\\Programs\\Python\\Python39',
 'C:\\Users\\smqia\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages']
Server time:    Fri, 05 Nov 2021 16:35:02  0000

CODE

settings.py

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

views.py

username = smqia404
password = ***************

user = authenticate(request, username=username, password=password, backend='django.contrib.auth.backends.ModelBackend')
login(request, user, backend='django.contrib.auth.backends.ModelBackend')

CodePudding user response:

authenticate returns None if the provided credentials are invalid and login expects a User object.

Thus, as docs instructs, you need to check if authenticate returned a user or not.

user = authenticate(request, username=username, password=password, backend='django.contrib.auth.backends.ModelBackend')
if user is not None:
    login(request, user, backend='django.contrib.auth.backends.ModelBackend')
else:
    # handle non-auth users

CodePudding user response:

if you getting the 'AnonymousUser' object error please check if the user is already saved in the Users Model in the Database or not. if not or credentials are mismating it tells the user was the 'AnonymousUser' other wise authentication system allows us to logging. you can also try login_requried decorator (login required to tell if a user is already existed in Users Models in Database).

to check if the user already existed in the Database or not using ORM.

In interpreter enter python manage.py shell

from django.contrib.auth.models import User
q=User.objects.filter("username"="enter username")
if len(q)>0:
   print("user existed")
   #try to login using authenticate function 
   # try to login with out authenticate function
   username=User.objects.get_or_404(username="your username")
   if username not None and username.password="your password":
       #login
       
   else:
       # user not found / credentials are mismatch.
else:
   
   #create user in Users Model(Handling if user doesn't exist)
   user = User.objects.create_user('username', 'email', 'password')
   user.save()

we believe you are missing the save method while registering a user. please check once.

if you need full code about this issue please post again will share it.

  • Related