django - 该页面未正确重定向

Ehs*_*san 1 django django-middleware django-allauth

我有一个检查用户配置文件的中间件.如果auth用户没有配置文件,则重定向到用户配置文件.我的浏览器显示错误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/')
Run Code Online (Sandbox Code Playgroud)

我用了django-allauth.

Ala*_*air 5

听起来你可能有一个无限的重定向循环.检查请求路径,如果用户尝试访问,请不要重定向/accounts/profile/.

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