更新时间:2023-11-28 22:55:10
你需要自定义模板标签:
You need custom template tag:
from django import template
register = template.Library()
@register.filter(name='has_group')
def has_group(user, group_name):
return user.groups.filter(name=group_name).exists()
在您的模板中:
{% if request.user|has_group:"mygroup" %}
<p>User belongs to my group
{% else %}
<p>User doesn't belong to mygroup</p>
{% endif %}
来源:http://www.abidibo.net/blog/2014/05/22/check-if-user-belongs-group-django-templates/
文档:https://docs.djangoproject.com/en/dev/howto/custom-template-tags/