且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查Django模板中的权限

更新时间:2023-11-27 19:11:34

如果您要查看模板中的权限,以下代码就足够了:

If you are looking to check for permissions in templates, the following code would suffice:

{% if perms.app_label.can_do_something %}
<form here>
{% endif %}

其中模型是指用户需要权限的模型请参阅

Where model refers to the model that the user need permissions to see the form for.

请参阅 https://docs.djangoproject.com/en/stable/topics/auth/default/#permissions 了解更多示例。

当前登录的用户权限存储在模板变量 {{perms}}

The currently logged-in user's permissions are stored in the template variable {{ perms }}

(这需要启用以下上下文处理器: django.contrib.auth.context_processors.auth

(This requires the following context processor to be enabled: django.contrib.auth.context_processors.auth)