django-页面无法正确重定向 [英] django-The page isn’t redirecting properly

查看:239
本文介绍了django-页面无法正确重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个检查用户个人资料的中间件。如果认证用户没有个人资料,则重定向到用户个人资料。我的浏览器显示错误页面未正确重定向

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

我正在使用 django-allauth

推荐答案

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

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

这篇关于django-页面无法正确重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆