0

我试图使用代码来强制登录,如果用户尚未验证。我尝试了太多的代码,因为它们大部分都是用旧版本的django编写的,所以我无法使它们工作。我正在使用django 1.10.3。整个网站不工作所需的django登录

运行服务器时我没有收到任何错误,但是我没有被重定向到登录页面,而我没有登录。我甚至不确定我们的MIDDLEWARE和MIDDLEWARE_ClASSES在设置文件中。任何帮助,将不胜感激。我也想有例外,我可以做出一些观点公开,无需登录

middleware.py的:

from builtins import hasattr, any 
from django.http import HttpResponseRedirect 
from django.conf import settings 
from re import compile 

EXEMPT_URLS = [compile(settings.LOGIN_URL.lstrip('/'))] 
if hasattr(settings, 'LOGIN_EXEMPT_URLS'): 
    EXEMPT_URLS += [compile(expr) for expr in settings.LOGIN_EXEMPT_URLS] 

class LoginRequiredMiddleware: 
    """ 
    Middleware that requires a user to be authenticated to view any page other 
    than LOGIN_URL. Exemptions to this requirement can optionally be specified 
    in settings via a list of regular expressions in LOGIN_EXEMPT_URLS (which 
    you can copy from your urls.py). 

    Requires authentication middleware and template context processors to be 
    loaded. You'll get an error if they aren't. 
    """ 
    def process_request(self, request): 
     assert hasattr(request, 'user'), "The Login Required middleware\ 
requires authentication middleware to be installed. Edit your\ 
MIDDLEWARE_CLASSES setting to insert\ 
'django.contrib.auth.middlware.AuthenticationMiddleware'. If that doesn't\ 
work, ensure your TEMPLATE_CONTEXT_PROCESSORS setting includes\ 
'django.core.context_processors.auth'." 
     if not request.user.is_authenticated(): 
      path = request.path_info.lstrip('/') 
      if not any(m.match(path) for m in EXEMPT_URLS): 
       return HttpResponseRedirect(settings.LOGIN_URL) 

setting.py:

MIDDLEWARE = [ 
    'django.middleware.security.SecurityMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
    'heaven.LoginRequiredMiddleware', 
] 
+1

如果你有'MIDDLEWARE'设置,Django会忽略你的'MIDDLEWARE_CLASSES'设置。 – knbk

+0

我将'heaven.middleware.LoginRequiredMiddleware'移到了MIDDLEWARE并删除了MIDDLEWARE_CLASSES。它给了我这个错误:文件“C:\ Python34 \ lib \ site-packages \ django \ core \ handlers \ base.py”,第82行,在load_middleware mw_instance = middleware(handler) TypeError:object()参数 – Ibo

+1

在'def process_request'下检查'assert'的缩进。 – pygeek

回答

1

Django在Django 1.10中改变了中间件逻辑。 Django 1.10有一个新的方法。 here

我的中间件类。它正在工作

# Below middleware class for Django1.10 
    class StatisticMiddleware: 
     def __init__(self, get_response): 
      self.get_response = get_response 
     # One-time configuration and initialization. 

     def __call__(self, request): 
      # Code to be executed for each request before 
      # the view (and later middleware) are called. 

      response = self.get_response(request) 

      self.__model_object_statistics(request, response) 

      return response 
     def __model_object_statistics(self, request, response): 
      # My algorithms