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