如何重定向尝试访问 Django 管理区域的用户?

发布于 2024-12-04 04:06:27 字数 328 浏览 1 评论 0原文

我注意到 Django 管理区域有一个有趣的问题。如果我撤销我的员工权限并尝试直接访问 /admin,我通常会期望重定向到我的登录页面,并在查询字符串中包含 /admin/ 作为未来的重定向。但是,我得到了一个带有 HTTP 代码 200 的正确页面,该页面实际上使用我的 admin/login.html 模板来呈现所请求的页面而不是重定向。问题似乎出在 @staff_member_required 装饰器中,管理视图显然使用了该装饰器。

问题是:这是故意的吗?如果不是,我怎样才能改变这种行为而不需要太多的猴子补丁?

I've noticed an interesting problem with Django's admin area. If I revoke my staff permissions and try to access /admin directly, I would normally expect a redirect to my login page with /admin/ in the query string as a future redirect. However, I get a proper page returned with HTTP code 200 which actually uses my admin/login.html template to render that requested page instead of redirecting. It seems the problem lies within the @staff_member_required decorator, which admin views obviously use.

The question is: is this done on purpose? If not, how can I change this behaviour without too much monkey-patching?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七月上 2024-12-11 04:06:27

这是故意这样做的,因为许多人在他们的网站中实施重定向,这可能会阻止对管理面板的访问。因为管理面板是它自己的应用程序,所以它会重定向到自身。

# Put this code somewhere it will be imported REALLY early

from django.contrib.admin.views import decorators

def staff_member_required(view_func):
    """
    Decorator for views that checks that the user is logged in and is a staff
    member, displaying the login page if necessary.
    """
    def _checklogin(request, *args, **kwargs):
        if request.user.is_active and request.user.is_staff:
            # The user is valid. Continue to the admin page.
            return view_func(request, *args, **kwargs)
        else:
            return HTTPResponseRedirect('/my/login/page/')
    return wraps(view_func)(_checklogin)

decorators.staff_member_required = staff_member_required #replaces the function in-place

This is done on purpose, because many people implement redirects in thier sites which could block access to the admin panel. Because the admin panel is it's own app it redirects to itself.

# Put this code somewhere it will be imported REALLY early

from django.contrib.admin.views import decorators

def staff_member_required(view_func):
    """
    Decorator for views that checks that the user is logged in and is a staff
    member, displaying the login page if necessary.
    """
    def _checklogin(request, *args, **kwargs):
        if request.user.is_active and request.user.is_staff:
            # The user is valid. Continue to the admin page.
            return view_func(request, *args, **kwargs)
        else:
            return HTTPResponseRedirect('/my/login/page/')
    return wraps(view_func)(_checklogin)

decorators.staff_member_required = staff_member_required #replaces the function in-place
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文