中间件进行无限重定向
middleware does an infinite redirect
我已经阅读 this 但答案不符合我的需要。
我制作了这个非常简单的中间件:
class RedirectIfUserIsNotActiveMiddleware(object):
"""
Middleware to redirect if account is disabled
"""
@staticmethod
def process_request(request):
if not request.user.is_active:
try:
# test to see if not already on the right url:
a = resolve(request.path)
if a.url_name != settings.URL_REDIRECT_USER_NOT_ACTIVE:
return HttpResponseRedirect(
reverse_lazy(settings.URL_REDIRECT_USER_NOT_ACTIVE))
# request.path do a 404 if URL unknown -> do nothing :
except Resolver404:
pass
我在这里添加了这个:
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'third_party.hqf_middleware.RedirectIfUserIsNotActiveMiddleware',
)
但是当帐户被禁用时,我在网络浏览器上收到一个无限循环消息:"mywebsite.com redirected you too many times."。
在我的设置中我有这个:
URL_REDIRECT_USER_NOT_ACTIVE = 'my_home_profile_account_desactivated'
而我想要得到的 URL 是 always(甚至每次都一步一步)/en/my-home/profile/edit
我错过了什么?
看起来 my_home_profile_account_desactivated
视图正在重定向到您的登录页面,然后中间件重定向回 my_home_profile_account_desactivated
,创建一个重定向循环。
您应该能够通过更改中间件来打破身份验证循环,以便您只重定向经过身份验证但不活动的用户。
@staticmethod
def process_request(request):
if request.user.is_authenticated() and not request.user.is_active:
...
我已经阅读 this 但答案不符合我的需要。
我制作了这个非常简单的中间件:
class RedirectIfUserIsNotActiveMiddleware(object):
"""
Middleware to redirect if account is disabled
"""
@staticmethod
def process_request(request):
if not request.user.is_active:
try:
# test to see if not already on the right url:
a = resolve(request.path)
if a.url_name != settings.URL_REDIRECT_USER_NOT_ACTIVE:
return HttpResponseRedirect(
reverse_lazy(settings.URL_REDIRECT_USER_NOT_ACTIVE))
# request.path do a 404 if URL unknown -> do nothing :
except Resolver404:
pass
我在这里添加了这个:
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'third_party.hqf_middleware.RedirectIfUserIsNotActiveMiddleware',
)
但是当帐户被禁用时,我在网络浏览器上收到一个无限循环消息:"mywebsite.com redirected you too many times."。
在我的设置中我有这个:
URL_REDIRECT_USER_NOT_ACTIVE = 'my_home_profile_account_desactivated'
而我想要得到的 URL 是 always(甚至每次都一步一步)/en/my-home/profile/edit
我错过了什么?
看起来 my_home_profile_account_desactivated
视图正在重定向到您的登录页面,然后中间件重定向回 my_home_profile_account_desactivated
,创建一个重定向循环。
您应该能够通过更改中间件来打破身份验证循环,以便您只重定向经过身份验证但不活动的用户。
@staticmethod
def process_request(request):
if request.user.is_authenticated() and not request.user.is_active:
...