django - 只允许 super 用户登录

标签 django django-admin

我写了一个django页面,只要求 super 用户登录。所以我加了

foo_view = staff_member_required(foo_view)

但这并没有削减,现在我只能控制只允许带有标记的人员登录,但这并没有削减。我已经尝试过类似的东西
def foo_view(request):
       if not request.user.is_superuser:
           #render some "not allowed page"
       #else render the page

但这似乎并没有给我带来错误。

最佳答案

尝试:

from django.contrib.auth.decorators import user_passes_test

def foo_view(request):
    # ...
foo_view = user_passes_test(lambda u: u.is_superuser)(foo_view)

或(使用Python> = 2.4):
from django.contrib.auth.decorators import user_passes_test

@user_passes_test(lambda u: u.is_superuser)
def foo_view(request):
    # ...

关于django - 只允许 super 用户登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1404160/

相关文章:

python - Django 管理员突然仅在生产环境中注销(拒绝执行脚本,因为其 MIME 类型 ('text/html' )不可执行)

python - 管理搜索时出现 Django 错误 : Cannot resolve keyword 'username' into field

python - 使用 django-import-export 反向外键

javascript - 在 Django 上下文变量上使用 Javascript 搜索算法

django - 如何在 Django 中使多对多字段可选?

python - 限制(许可)Django 员工用户更改模型的某些字段

python - Django REST Framework 基于 URL 过滤查询集

python - 无法在 Apache、WSGI、Django 和 Windows 中加载虚拟环境

ios - 带有django csrf的ionic/angularjs可以在本地服务器上工作,但不能在设备上工作

django - 将Django应用程序重新部署到Google Kubernetes Engine(GKS)