【问题标题】:Is request.user.is_authenticated() necessary here?request.user.is_authenticated() 这里有必要吗?
【发布时间】:2013-12-24 08:42:43
【问题描述】:

我正在为员工用户编写视图。

def my_view(request):
    if request.user.is_staff:
        # show something to staff
    else:
        return my_view_404(request)

这个权限检查就够了吗?有什么破绽吗?还是我应该检查 request.user.is_authenticated()?

【问题讨论】:

    标签: django authentication django-authentication


    【解决方案1】:

    您无需检查is_authenticated()。未经身份验证的用户将在request.user 中有一个AnonymousUser 对象,并且该类型的对象应无一例外地响应False 对所有is_staff 检查。

    【讨论】:

      【解决方案2】:

      不需要,因为 request.user.is_staff 会检查身份验证和人员状态。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-06
        • 2023-03-27
        • 1970-01-01
        • 2013-11-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多