[Solved]-How do I change a Django Template based on the User's Group?


This is probably too old for you to care any more, but I stumbled here myself before figuring it out on my own. For posterity, I found the following solution:

In your view, add something like this:

is_customer = request.user.groups.filter(name='Customers').exists()

In your template:

{% if is_customer %} customer stuff here {% endif %}

It relies on the fact that an if clause in a template will be evaluate to false for an empty list.


If you want to avoid adding anything to your view functions, and you’re using the auth context processor (django.contrib.auth.context_processors.auth) and RequestContext as per @thyagx’s answer, then you could use a template snippet like that suggested in this Google Groups post:

{% for group in user.groups.all %}
    {% if group.name == 'customers' %}
        {% comment %}Customer-specific code goes here{% endcomment %}
    {% endif %}
{% endfor %}

It’s a bit verbose, but it does mean you don’t have to do anything in your view (aside from using RequestContext), or write a custom context processor.


What I did to solve this is:

  1. I created a custom context processor which basically inserts new variables for you to use in your templates and added it to my settings. See more @ https://docs.djangoproject.com/en/1.3/ref/templates/api/#django.template.RequestContext:

  2. I went on to write the function user_context inside the file user_context, mine is like so:

    def user_context(request):
        if request.user.is_authenticated():
            is_admin = is_local_admin(request.user)
            is_admin = False
        return {
            'is_local_admin': is_admin

    is_local_admin is just a function that checks if the user belongs to the Admins group or not.

  3. Whenever I need this is_local_admin information in my template I use this to render it in my view, for example:

    return render_to_response('create_user.html', {
        'form': form
    }, context_instance=RequestContext(request))

The important part is the RequestContext, which loads the custom context processor we build in step 1.

Now in your template you can just use:

{% if is_local_admin %}
    <h1>You can see this</h1>
{% else %}
    <h1>Nothing here</h1>
{% endif %}

Hope this helps someone. In summary: take a look at custom context processors, they are worth the read.



I implemented this through a template tag, based on what I found here.
Perhaps it can be useful to someone.

In utils/utils_extras.py:

from django import template
from django.contrib.auth.models import Group

register = template.Library()
def has_group(user, group_name):
        group = Group.objects.get(name=group_name)
        return False  # group doesn't exist, so for sure the user isn't part of the group

    # for superuser or staff, always return True
    if user.is_superuser or user.is_staff:
        return True

    return user.groups.filter(name=group_name).exists()

Then in the template itself:

{% load utils_extras %}
{% if user|has_group:"mygroup" %}


{% if  'group_name' in user.groups.get.name %} do smth {% endif %}

Leave a comment