django-页面没有正确重定向

django-The page isn’t redirecting properly

我有一个检查用户配置文件的中间件。如果 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/')

我正在使用 django-allauth

听起来您可能遇到了无限重定向循环。检查请求路径,如果用户试图访问 /accounts/profile/.

,则不要重定向
class Check(MiddlewareMixin):
    def process_request(self, request):
        if request.user.is_authenticated() and request.path != '/accounts/profile/':
            ...

确保您使用的是正确的视图,在我的例子中我使用的是:

查看

class Blog_View(View):
    model = Blog
    template_name = 'blog.html'

而不是

列表视图

class Blog_View(ListView):
    model = Blog
    template_name = 'blog.html'