Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在django html模板中排除某些特定的django权限_Django_User Permissions - Fatal编程技术网

如何在django html模板中排除某些特定的django权限

如何在django html模板中排除某些特定的django权限,django,user-permissions,Django,User Permissions,我试图显示与我的项目相关的用户权限,不包括一些默认的django用户权限。我正在实现以下代码。我想从html模板中排除会话、内容类型、组等权限。我该怎么做 views.py 模板 我想删除用户可以添加组,用户可以在模板中更改组 如果检查权限对象的字段,可以找到名为content\u type的字段。内容类型指定应用程序标签和模型,您可以从中排除为用户、组、会话等定义的权限。 例如,您可以找到用户、组、会话等模型的内容类型ID,如下所示: from django.contrib.contentty

我试图显示与我的项目相关的用户权限,不包括一些默认的django用户权限。我正在实现以下代码。我想从html模板中排除会话、内容类型、组等权限。我该怎么做

views.py

模板

我想删除用户可以添加组,用户可以在模板中更改组


如果检查权限对象的字段,可以找到名为
content\u type
的字段。内容类型指定
应用程序标签
模型
,您可以从中排除为用户、组、会话等定义的权限。

例如,您可以找到用户、组、会话等模型的内容类型ID,如下所示:

from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
content_type_ids = []  # a list to store the ids of the content_type object which you want to exclude

# for user model
content_type_ids.append(ContentType.objects.get(model='user').id)
# for session model
content_type_ids.append(ContentType.objects.get(model='session').id)
# for group model
content_type_ids.append(ContentType.objects.get(model='group').id)

# exclude the Permissions having content_type_id obtained
permissions = Permission.objects.exclude(content_type_id__in=content_type_ids)
通过这种方式,您可以获取不希望在模板中显示的每个模型的内容类型ID,并将其排除在外

我自己也尝试过,虽然这是一个漫长的过程,但我希望其他人有更好的解决方案,更有效、更快

{% for permission in permissions %}
{{permission.name}}
{% endfor %}
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
content_type_ids = []  # a list to store the ids of the content_type object which you want to exclude

# for user model
content_type_ids.append(ContentType.objects.get(model='user').id)
# for session model
content_type_ids.append(ContentType.objects.get(model='session').id)
# for group model
content_type_ids.append(ContentType.objects.get(model='group').id)

# exclude the Permissions having content_type_id obtained
permissions = Permission.objects.exclude(content_type_id__in=content_type_ids)