django-The page isn’t redirecting properly

被刻印的时光 ゝ 提交于 2019-12-11 07:01:50

问题


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.


回答1:


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/':
            ...


来源:https://stackoverflow.com/questions/42674973/django-the-page-isn-t-redirecting-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!