![](/img/trans.png)
[英]Getting 'ERR_TOO_MANY_REDIRECTS' error when using custom Middleware in Django
[英]ERR_TOO_MANY_REDIRECTS in redirect with middleware in Django
我编写了这个程序,以便在满足某些条件时将用户移动到我的目标页面。
这是我的自定义 Django middleware
:
def check_userprofile_middleware(get_response):
def middleware(request):
response = get_response(request)
if request.user.is_authenticated:
# Profile conditions goes here.
if profile_condition:
return redirect(reverse('base:edit_user_profile'))
return response
return middleware
如果我在if
语句中使用return
,它会重定向到 'base:edit_user_profile' url,但之后我在浏览器上看到此错误:
This page isn’t working
127.0.0.1 redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS
如果我不在if
语句中使用return
,一切都会正确,除了重定向!
这有什么问题?
通过@gelonida 的提示,我收到了答案。 我确实添加了这一行,问题得到解决:
while not (request.path == reverse('base:edit_edupanel_user_profile')):
return redirect(reverse('base:edit_edupanel_user_profile'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.