How to check (in template) if user belongs to a group

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() 

In your template:

{% if request.user|has_group:"mygroup" %} 
    <p>User belongs to my group 
{% else %}
    <p>User doesn't belong to mygroup</p>
{% endif %}

Source: http://www.abidibo.net/blog/2014/05/22/check-if-user-belongs-group-django-templates/

Docs: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)