django - 页面没有正确重定向

问题描述:

我有一个检查用户配置文件的中间件。如果auth用户没有配置文件,则重定向到用户配置文件。我的浏览器显示错误The page isn’t redirecting properlydjango - 页面没有正确重定向

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/') 

我使用django-allauth

+0

我相信里面'HttpResponseRedirect'你应该使用['reverse'(https://docs.djangoproject.com/en/dev/ref/urlresolvers/#django.urls.reverse)功能。这应该做到这一点。 –

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

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

坦克我的兄弟 – Ehsan