DjangoDjango

Checking Current User Permissions in Django Templates

Mar 18, 2025 · by Tim Kamanin

When developing Django applications, we often need to control which parts of the UI are accessible based on user permissions. Luckily, Django provides a straightforward way to check user permissions directly in templates using the perms object.

Using the perms Object in Django Templates

Django automatically injects a perms context variable into templates, allowing you to check if a user has specific permissions. The syntax follows this pattern:

{% if perms.app_label.permission_codename %}
    <!-- Content visible only to users with this permission -->
{% endif %}

In my case, I needed to check whether I should display a Wagtail Admin link to the current user or not.

In Wagtail, admin access permission has the permission codename access_admin, and it belongs to the wagtailadmin app. Therefore, in my template, I did the following to achieve my goal:

<ul>
    {% if perms.wagtailadmin.access_admin %}
        <li>
            <a href="{% url 'wagtailadmin_home' %}">
                Admin panel
            </a>
        </li>
    {% endif %}
</ul>

How It Works

  • perms.wagtailadmin.access_admin checks if the current user has the access_admin permission for the wagtailadmin app.
  • If the user has this permission, the Admin panel link is displayed.
  • If the user does not have this permission, the link remains hidden.

As you can see, Django, as always, has got your back, and its perms object provides an easy way to manage user access within templates. Thank you, Django!

Hey, if you've found this useful, please share the post to help other folks find it:

There's even more:

Subscribe for updates

  • via Twitter: @timonweb
  • old school RSS:
  • or evergreen email ↓ ↓ ↓