Search code examples
djangodjango-allauthdjango-middleware

django-The page isn’t redirecting properly


I have a middleware that check user profile. If auth user doesn't have a profile, then redirect to user profile. My browser displays the error The page isn’t redirecting properly.

class Check(MiddlewareMixin):
    def process_request(self, request):
        if request.user.is_authenticated():
            user = request.user
            try:
                profile = Profile.objects.get(user_id = user)
                if profile:
                    pass
            except ObjectDoesNotExist:
                return HttpResponseRedirect('/accounts/profile/')

I'm use django-allauth.


Solution

  • It sounds like you might have an infinite redirect loop. Check the request path, and do not redirect if the user is trying to access /accounts/profile/.

    class Check(MiddlewareMixin):
        def process_request(self, request):
            if request.user.is_authenticated() and request.path != '/accounts/profile/':
                ...