title: 'AnonymousUser' object has no attribute '_meta' | Django
body: <p>Errors occurs while authenticating user,</p>
  <p><strong>CODE</strong></p>
  <p><strong>settings.py</strong></p>
  <pre><code>AUTH_USER_MODEL = 'Authentication.User'
  
  AUTHENTICATION_BACKENDS = (
      ('django.contrib.auth.backends.ModelBackend'),
      ('Authentication.auth.CustomAuthEmailBackend.EmailAuthBackend'),
  )
  </code></pre>
  <p><strong>Custom Backend</strong></p>
  <pre><code>from django.contrib.auth.models import User
  from django.contrib.auth.backends import BaseBackend
  
  
  class EmailAuthBackend(BaseBackend):
  
      def Authenticate(self, request, email, password):
          try:
              user = User.objects.get(email=email)
              success = user.check_password(password)
  
              if success:
                  return user
  
          except User.DoesNotExist:   
              pass
          return None
  
      def get_user(self, uid):
          try:
              return User.objects.get(pk=uid)
          except:
              return None
  </code></pre>
  <p><strong>views.py</strong></p>
  <pre><code>    user_id = 1
      password = *******
      user = User.objects.get(pk=user_id)
      user.unique_username = unique_username
      userid = int(user_id)
      user.custom_user_id = createUserUnqiueId(userid)
      user.is_active = True
      useremail = user.email
      user.save()
  
      user = auth.authenticate(request, email=useremail, password=password, backend='Authentication.auth.CustomAuthEmailBackend.EmailAuthBackends')
      auth.login(request, user, backend='Authentication.auth.CustomAuthEmailBackend.EmailAuthBackend')
  
  
      return redirect('index')
  </code></pre>
  <p><strong>CODE EXPLANATION</strong></p>
  <ul>
  <li>Working with email login by overriding username login.</li>
  <li>Then in authentication I have coded a custom for authentication but sometimes it works fine and default gives error and sometimes custom authentication back-end displays error and default authentication back-end works fine. Why?</li>
  <li>Also, if an authentication back-end is working fine on localhost, it displays errors after uploading on C panel.</li>
  </ul>
  <p><strong>ERROR</strong></p>
  <p><a href="https://i.stack.imgur.com/WX8yg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WX8yg.png" alt="enter image description here" /></a></p>
  <pre><code>AttributeError at /auth/activate/Mjc/atr5z0-1d1d9f0050ff97fec5e766fd2418b520/s%2523f%253FO%253E%2560!c%257CK-0B92
  'AnonymousUser' object has no attribute '_meta'
  Request Method: POST
  Request URL:    http://app.barter.monster/auth/activate/Mjc/atr5z0-1d1d9f0050ff97fec5e766fd2418b520/s%252523f%25253FO%25253E%252560!c%25257CK-0B92
  Django Version: 3.2.7
  Exception Type: AttributeError
  Exception Value:    
  'AnonymousUser' object has no attribute '_meta'
  Exception Location: /home4/barter/virtualenv/Barter/3.8/lib/python3.8/site-packages/django/utils/functional.py, line 247, in inner
  Python Executable:  /home4/barter/virtualenv/Barter/3.8/bin/python3.8
  Python Version: 3.8.6
  Python Path:    
  ['',
   '/opt/alt/python38/bin',
   '/home4/barter/Barter',
   '/home4/barter/virtualenv/Barter/3.8/lib64/python38.zip',
   '/home4/barter/virtualenv/Barter/3.8/lib64/python3.8',
   '/home4/barter/virtualenv/Barter/3.8/lib64/python3.8/lib-dynload',
   '/opt/alt/python38/lib64/python3.8',
   '/opt/alt/python38/lib/python3.8',
   '/home4/barter/virtualenv/Barter/3.8/lib/python3.8/site-packages']
  Server time:    Wed, 29 Sep 2021 20:00:14 +0000
  </code></pre>