About Checking Permissions with django-guardian in a Template Generically

Django Guardian template tag {% get_obj_perms %} returns a list of allowed permission codenames, such as "change_song" or "change_video". If your template accepts multiple types of objects, it might seem tricky to check if "change_thatthing" is in the list of permissions. But you can still do it by checking for the existence of "change_" in the list, joined to a string:

1
2
3
4
5
6
7
8
9
{% load guardian_tags %}

{% get_obj_perms request.user for obj as "obj_perms" %}

{% if "change_" in obj_perms|join:"|" %}
    <a href="{{ obj.get_change_url }}">
        Change {{ obj.title }}
    </a>
{% endif %}

Tips and Tricks Programming Permissions Django 4.2 Django 3.2 Django 2.2 django-guardian