整个网站无法正常工作所需的 django 登录
django login required for the entire site not working
如果用户尚未通过身份验证,我正在尝试使用代码强制登录。我尝试了太多的代码,但我无法让它们工作,因为它们中的大多数是用旧版本的 django 编写的。我正在使用 Django 1.10.3.
当 运行 服务器时我没有收到任何错误,但是当我没有登录时我也没有被重定向到登录页面。我什至不确定我们是否可以同时拥有中间件和 MIDDLEWARE_ClASSES 在设置文件中。任何帮助,将不胜感激。我也想有例外,我可以制作一些不需要登录的视图 public
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',
]
Django 在 Django 1.10 更改了中间件逻辑。 Django 1.10 有一个新方法。
here
我的中间件class。正在运行
# 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
如果用户尚未通过身份验证,我正在尝试使用代码强制登录。我尝试了太多的代码,但我无法让它们工作,因为它们中的大多数是用旧版本的 django 编写的。我正在使用 Django 1.10.3.
当 运行 服务器时我没有收到任何错误,但是当我没有登录时我也没有被重定向到登录页面。我什至不确定我们是否可以同时拥有中间件和 MIDDLEWARE_ClASSES 在设置文件中。任何帮助,将不胜感激。我也想有例外,我可以制作一些不需要登录的视图 public
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',
]
Django 在 Django 1.10 更改了中间件逻辑。 Django 1.10 有一个新方法。 here
我的中间件class。正在运行
# 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