ERR_TOO_MANY_REDIRECTS 在 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'))