programador21
programador21

Reputation: 45

How to check in Django template if user has delete permission?

I would like to check if the user has permission to delete an object in the template. If the user has permission, I display or enable the button. Permissions are per group. How can I implement this in the Django template?

{% if ... %} 
    <button><a href="/delete">Delete</a></button>
{% endif %}

Upvotes: 2

Views: 1123

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477308

The permissions of the user are stored in the perms variable, as specified in the documentation. You thus can check if a user has a delete permission for a model with:

{% if perms.app_name.delete_model_name %}
    …
{% endif %}

with app_name and modelname the name of the app and the model respectively.


Note: It is advisable to make use of the {% url … %} template tag [Django-doc] where the URL is resolved based on the name of a view instead of hardcoding URLs in the template.

Upvotes: 2

Related Questions