0

I am currently new to django framework and I am making a Middleware for authentic redirection of pages within a website. So if the user is guest user I want to always make him redirect to login page. But I came with this error. I have tried HttpResponse and return redirect but it didn't worked for me.

I am getting this Attribute error at all the pages I am accessing to This shows me along with the error "'str' object has no attribute 'get'"

My MiddleWare Code is:

import re

from django.shortcuts import redirect
from django.conf import settings
from django.contrib.auth import logout
from django.urls import reverse
from django.http import HttpResponse

EXEMPT_URLS = [re.compile(settings.LOGIN_URL.lstrip('/'))]

if hasattr(settings, 'LOGIN_EXEMPT_URLS'):
EXEMPT_URLS += [re.compile(url) for url in settings.LOGIN_EXEMPT_URLS]

class LoginRequiredMiddleware:
   def __init__(self, get_response):
       self.get_response = get_response

   def __call__(self, request):
       response = self.get_response(request)
       return response

   def process_view(self, request, view_func, view_args, view_kwargs):
       assert hasattr(request, 'user')
       path = request.path_info.lstrip('/')
       print(path)

      #if not request.user.is_authenticated:
      #    if not any(url.match(path) for url in EXEMPT_URLS):
      #        return redirect(settings.LOGIN_URL)

      url_is_exempt = any(url.match(path) for url in EXEMPT_URLS)
      if path == reverse('logout').lstrip('/'):
          logout(request)
          return redirect('/accounts/')

      if request.user.is_authenticated and url_is_exempt:
          return redirect(settings.LOGIN_REDIRECT_URL)

      elif request.user.is_authenticated or url_is_exempt:
          return None

      else:
          return(settings.LOGIN_URL)

This is my link of the error https://ibb.co/MDZSWWJ

enter image description here

stuart
  • 57
  • 6

0 Answers0