Home > front end >  How to check in Django template if user has delete permission?
How to check in Django template if user has delete permission?

Time:06-06

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 %}

CodePudding user response:

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.

  • Related