ofnowhere
ofnowhere

Reputation: 1135

Remove history button from Django admin

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.

Upvotes: 7

Views: 5818

Answers (4)

Pouya Esmaili
Pouya Esmaili

Reputation: 51

I came across this and I wasn't satisfied with the original answers. So I did a bit of digging and found out this solution. You can override render_change_form and call the super while setting change=False like this:

def render_change_form(
    self, request, context, add=False, change=False, form_url="", obj=None
):
    return super().render_change_form(
        request, context, add, False, form_url, obj
    )

Upvotes: -1

Markmeplease
Markmeplease

Reputation: 145

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.11\site-packages\django\contrib\admin\templates\admin\

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

Upvotes: 0

elsadek
elsadek

Reputation: 1086

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

Upvotes: 1

taras
taras

Reputation: 3705

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)

Upvotes: 5

Related Questions