Skip to content

Instantly share code, notes, and snippets.

@RaD
Created October 15, 2013 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RaD/6988265 to your computer and use it in GitHub Desktop.
Save RaD/6988265 to your computer and use it in GitHub Desktop.
django in_group template tag
# -*- coding: utf-8 -*-
from django import template
from django.utils.encoding import force_unicode
register = template.Library()
@register.filter
def in_group(user, groups):
"""Returns a boolean if the user is in the given group, or comma-separated
list of groups.
Usage::
{% if user|in_group:"Friends" %}
...
{% endif %}
or::
{% if user|in_group:"Friends,Enemies" %}
...
{% endif %}
"""
group_list = force_unicode(groups).split(',')
return bool(user.groups.filter(name__in=group_list).values('name'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment