Search code examples
pythondjangodjango-middleware

ERR_TOO_MANY_REDIRECTS in redirect with middleware in Django


I wrote this program so that the user would be moved to my target page if it had certain conditions.

This is my custom 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 I use return in if statement, it redirects to 'base:edit_user_profile' url, But after that I see this error on browser:

This page isn’t working
127.0.0.1 redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS

If I don't use return in if statement, everything goes correct,except redirection!

What is wrong with this?


Solution

  • By tips of @gelonida I recieve to answer. I did add this line and problem was solve:

            while not (request.path == reverse('base:edit_edupanel_user_profile')):
                return redirect(reverse('base:edit_edupanel_user_profile'))