title: Django fails to login user
body: <p>Authenticating a user using valid credentials passes on the login view. When <code>request.user.is_authenticated</code> is printed, it even outputs True. But after redirecting to <code>LOGIN_REDIRECT_URL</code>, the request is returned back to <code>LOGIN_URL</code> meaning the just authenticated user is no longer authenticated.</p>
  <p>The project in concern is a mixed project i.e It uses core django builtin view, route and templating as well as djangorestframework.</p>
  <p>djangorestframework TokenAuthentication has worked fine so far, but when i try logging in with core django view i get this weird situation.</p>
  <p>I'm using a custom auth user model and two backend authentication ( which are all working fine on api authentication and authorization ). Every other aspect of the project works fine, the only issue is this authentication. I've tried several resource to fix this, but all lead to dead ends. I even used functional based view and django builtin LoginView (as an inherited class and on path route ), same occurence i got irrespective.</p>
  <h1>Tools</h1>
  <ul>
  <li><p>Django 3.1.1</p>
  </li>
  <li><p>Python 3.8.4</p>
  </li>
  <li><p>Postgres</p>
  </li>
  <li><p>Postgis</p>
  </li>
  <li><p>django restframework 3.11.1</p>
  </li>
  </ul>
  <p><strong>settings.py</strong></p>
  <pre class="lang-py prettyprint-override"><code>INSTALLED_APPS = [
      'django.contrib.auth',
      'django.contrib.contenttypes',
      'django.contrib.sessions',
      'django.contrib.staticfiles',
      'django.contrib.gis',
  
       # installed apps and third party apps
  ]
  
  
  MIDDLEWARE = [
      'django.middleware.security.SecurityMiddleware',
      'django.contrib.sessions.middleware.SessionMiddleware',
      'corsheaders.middleware.CorsMiddleware',
      'django.middleware.common.CommonMiddleware',
      'django.middleware.csrf.CsrfViewMiddleware',
      'django.contrib.auth.middleware.AuthenticationMiddleware',
      'django.contrib.messages.middleware.MessageMiddleware',
      'django.middleware.clickjacking.XFrameOptionsMiddleware',
  ]
  
  
  AUTH_USER_MODEL = 'accounts.Account'
  PASSWORDLESS_AUTH_BACKEND = 'accounts.auth_backends.PasswordLessAuthenticationBackend'
  EMAIL_AUTHENTICATION_BACKEND = 'accounts.auth_backends.EmailAuthenticationBackend'
  
  AUTHENTICATION_BACKENDS = (
      EMAIL_AUTHENTICATION_BACKEND, PASSWORDLESS_AUTH_BACKEND
  )
  
  LOGIN_URL = '/admin/'
  LOGIN_REDIRECT_URL = '/admin/index'
  
  REST_FRAMEWORK = {
      'DEFAULT_AUTHENTICATION_CLASSES': (
          'rest_framework_simplejwt.authentication.JWTAuthentication',
      )
  }
  
  SIMPLE_JWT = {
      'ACCESS_TOKEN_LIFETIME': timezone.timedelta(days=1),
      'REFRESH_TOKEN_LIFETIME': timezone.timedelta(days=2),
      'ROTATE_REFRESH_TOKENS': False,
      'BLACKLIST_AFTER_ROTATION': True,
  
      'SIGNING_KEY': os.getenv('PROJECT_SECRET_KEY'),
      'ALGORITHM': 'HS256',
      'VERIFYING_KEY': None,
      'AUDIENCE': None,
      'ISSUER': None,
  
      'AUTH_HEADER_TYPES': ('Bearer',),
      'USER_ID_FIELD': 'id',
      'USER_ID_CLAIM': 'user_id',
  
      'AUTH_TOKEN_CLASSES': ('rest_framework_simplejwt.tokens.AccessToken',),
      
      ...
  }
  </code></pre>
  <p><strong>views.py</strong></p>
  <pre class="lang-py prettyprint-override"><code>class WebAdminLoginView(View):
      &quot;&quot;&quot;Handles web admin login&quot;&quot;&quot;
  
      def post(self, request, *args, **kwargs):
          &quot;&quot;&quot;logs in authenticated user account.&quot;&quot;&quot;
          email, password = request.POST['email'], request.POST['password']
          account = authenticate(email=email, password=password)
          login(request, account, backend=settings.EMAIL_AUTHENTICATION_BACKEND)
         
          # The below code outputs True
          print(request.user.is_authenticated)            # printsTrue
          return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
  
      def get(self, request, *args, **kwargs):
          return render(request, &quot;admin_login.html&quot;)
  
  
  class IndexListing(LoginRequiredMixin, ListView):
      &quot;&quot;&quot;Returns a list of all verified partners&quot;&quot;&quot;
      model = User
      context_object_name = 'users'
      template_name = 'jadminapp/index/list_users.html'
  </code></pre>
  <p><strong>urls.py</strong></p>
  <pre class="lang-py prettyprint-override"><code>index_patterns= [
      path(...),
      ....
  ]
  
  urlpatterns = [
      path(
          '',
          core_view.WebAdminLoginView.as_view(),
          name='web-admin-login'
      ),
  
      path(
          'index', 
          include(
              (index_patterns, 'index'), 
              namespace='index'
          )
      ),
  ]
  </code></pre>