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:

{% 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 6.x Django 5.2 Django 4.2 django-guardian

Django/Python Consulting

If you have a specific Django challenge or integration you'd like to solve, I'd be happy to help. Book a free 30-minute call to discuss your project, see if we're a good fit, and explore the best approach for your needs. After the call, you'll receive a tailored cost estimate based on what we discuss.