Hide CRUD buttons in Django templates for users who are not logged into the system

2

I would like to hide the buttons on my system that allow you to: add, edit and delete the data in my templates for users who are not logged into the system. It can be viewed here: link

After some queries, I saw that it can be done this way:

{% if request.user.is_authenticated %}
    ...
    <button>...</button>
    ...
{% else %}
...
{% endif %}

However, several conditions will be added to the templates ... would you have some more elegant way of doing this?

    
asked by anonymous 25.04.2016 / 14:22

1 answer

0

I succeeded, following the hint that was passed. I have reused the logic in the templates, from includes, as follows:

 {% include 'partials/actions.html' with pk=basin.pk detail='basins:detail' edit='basins:edit' delete='basins:delete' %}

and no include:

<a href="{% url detail pk %}" class="btn btn-primary btn-sm" role="button">
    <span class="glyphicon glyphicon-eye-open" aria-hidden="true" title="Ver"></span>
</a>

{% if request.user.is_authenticated %}

    <a href="{% url edit pk %}" class="btn btn-primary btn-sm" role="button">
        <span class="glyphicon glyphicon-edit" aria-hidden="true" title="Editar"></span>
    </a>
    <a href="{% url delete pk %}" class="btn btn-primary btn-sm" role="button">
        <span class="glyphicon glyphicon-trash" aria-hidden="true" title="Deletar"></span>
    </a>

{% endif %}
    
28.04.2016 / 01:00