我已经阅读了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',
)
但是当帐户被禁用时,我会在webbrowser上收到一条无限循环信息:" mywebsite.com重定向了你太多次了。"。
在我的设置中,我有这个:
URL_REDIRECT_USER_NOT_ACTIVE = 'my_home_profile_account_desactivated'
我想要获取的网址始终(即使每次都是一步一步)/en/my-home/profile/edit
我错过了什么?
答案 0 :(得分:0)
看起来好像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:
...