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