Remove history button from Django admin

Question:

I want to enable/disable history from django admin button based on the type of user.

enter image description here

My end goal here is to be able to understand how to show hide this button.

Asked By: ofnowhere

||

Answers:

Unfortunately, Django does not provide an easy way to toggle History button like it is done for ‘Add’ button, for instance. The easiest way would be to overwrite a change_form.html and remove the next lines from block object-tools-items:

<li>
        {% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
        <a href="{% add_preserved_filters history_url %}" class="historylink">{% trans "History" %}</a>
</li>

Keep in mind that you have to specify change_form for every admin model.
Example:

class TestAdmin(admin.ModelAdmin):
    # path to the app_name/templates/admin/app_name/change_form.html
    change_form_template = 'admin/app_name/change_form.html'

# Register your models here.
admin.site.register(Test, TestAdmin)
Answered By: taras

A clean solution would be to override change_form_object_tools.html template, which needs to be placed in templates/admin/ of your project.

    {% load i18n admin_urls %}
    {% block object-tools-items %}

    {% block comment %}
     <li>
        {% url opts|admin_urlname:'history' original.pk|admin_urlquote as   history_url %}
        <a href="{% add_preserved_filters history_url %}" class="historylink">
 {% translate "History" %}</a>
    </li>
    {% endcomment %}

    {% if has_absolute_url %}<li><a href="{{ absolute_url }}" class="viewsitelink">{% translate "View on site" %}</a></li>{% endif %}
    {% endblock %}
Answered By: elsadek

I know this is old. I also encounter this concern, and based on the answer of @elsadek and the comment of @daigorocub, this is how I manage to show/hide the history button/link.

create the admin template to override the default admin template for the change_form_object_tools.html, the directory would be app/templates/admin/.

this is my change_form_object_tools.html looks like. it is different from the default change_form_object_tools.html of django because I am using django-jazzmin for admin template, but the process is just the same.

{% load i18n admin_urls jazzmin %}
{% get_jazzmin_ui_tweaks as jazzmin_ui %}

{% block object-tools-items %}
    {% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}

    # Validate if the user is super user, if true, the history button is displayed, else, remove the history button.
{% if request.user.is_superuser %} 
    <a class="btn btn-block {{ jazzmin_ui.button_classes.secondary }} btn-sm" href="{% add_preserved_filters history_url %}">{% trans 'History' %}</a>
    {% endif%}
    {% if has_absolute_url %}
        <a href="{{ absolute_url }}" class="btn btn-block {{ jazzmin_ui.button_classes.secondary }} btn-sm">{% trans "View on site" %}</a>
    {% endif %}
{% endblock %}

for reference, this is the default change_form_object_tools.html of django which can be found in ..python3.11site-packagesdjangocontribadmintemplatesadmin

change_form_object_tools.html

{% load i18n admin_urls %}
{% block object-tools-items %}
<li>
    {% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
    <a href="{% add_preserved_filters history_url %}" class="historylink">{% translate "History" %}</a>
</li>
{% if has_absolute_url %}<li><a href="{{ absolute_url }}" class="viewsitelink">{% translate "View on site" %}</a></li>{% endif %}
{% endblock %}
Answered By: Markmeplease