当用户未通过身份验证时,我正在尝试重定向到登录页面.在我的 settings.py 类中,我有:
I'm trying to redirect to the login page when a user is not authenticated. In my settings.py class I have:
MIDDLEWARE_CLASSES = [ 'path.to.AuthRequiredMiddleware', ]这是我的课程:
class AuthRequiredMiddleware(object): def process_request(self, request): if not request.user.is_authenticated(): return HttpResponseRedirect('/admin/login/') return None但是,这始终会导致 [24/Jan/2017 14:09:07]当用户未通过身份验证时,"GET/admin/login/HTTP/1.1" 302 0 如何解决这个问题?无论重定向URL为何,更改都会导致相同的问题.
However this always results in [24/Jan/2017 14:09:07] "GET /admin/login/ HTTP/1.1" 302 0 when the user is not authenticated, does anyone know how to fix this? Changing the redirection URL results in the same issue no matter what it is.
我也尝试过使用 django.shortcuts导入重定向,但是从中我也得到了相同的 302 结果.
I have also tried to use the django.shortcuts import redirect however I got the same 302 result as well from it.
推荐答案如果这是您的全部代码,那么您总是会陷入重定向循环.
If this is your whole code then you always end up in a redirect loop.
因此/admin/login 应该是一个例外,用户可以在未登录时获取该信息.
So /admin/login should be an exception where user can get when he is not logged in.
类似这样的东西:
class AuthRequiredMiddleware(object): def process_request(self, request): redirect_url = '/admin/login' if not request.user.is_authenticated() and request.path != redirect_url: return HttpResponseRedirect(redirect_url) return None更多推荐
重定向到/admin/login/结果为302
发布评论