Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

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

  • You need a 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 %}
Reference: https://stackoverflow.com/questions/34571880/how-to-check-in-template-if-user-belongs-to-a-group