2017-03-08 136 views
0

我有一個檢查用戶配置文件的中間件。如果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)功能。這應該做到這一點。 –

回答

3

這聽起來像你可能有一個無限的重定向循環。檢查請求路徑,如果用戶試圖訪問/accounts/profile/,則不要重定向。

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

坦克我的兄弟 – Ehsan