6. Template Permissions

6.1. Overview

There are two helpers for checking a users permission against roles from within a template, this makes it easy to include functionality into the template for those users that have a certain role and hide it from those without that role.

6.2. Usage - Template Tags

The template tag implements a way of choosing what the currently logged in user will see

class portal.utils.templatetags.permissionrequired.permissionrequired(parser, token)
{% permissionrequired _special_all %}

<p>Show if user has the _special_all permission</p>

{% else % }

<p>Show this if they don’t have the permission</p>

{% endpermissionrequired %} The template tag permissionrequired takes one or more arguments of roles to be matched. If user has any of the listed roles the normal content is rendered.

6.3. Usage - Filter

class portal.utils.templatetags.permissionrequired.has_role(user, roles)

Returns a boolean if the user has the give role, or comma-separated list of roles. Can be used in conjunction with the standard if tag.


{% if user|has_role:"Friends" %}
{% endif %}


{% if user|has_role:"Friends,Enemies" %}
{% endif %}
class portal.utils.templatetags.permissionrequired.get_logged_in_roles(user)

Returns users logged in roles.