Disable Django model admin buttons (save, delete, etc.)

Question:

I would like to disable all buttons that they are present into the submit-row (save, save and edits, delete, etc…) after the click on one of they.

I started to try to override the change_form for admin model. Something
like this:

class MyAdmin(admin.ModelAdmin):
    change_form_template = 'admin/reports/models/change_form.html'

into the admin/reports/models/change_form.html file I added this code:

{% extends "admin/change_form.html" %}

{% load i18n admin_urls %}
<div class="submit-row">
{% block submit-row %}
{% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_save">{% endif %}
{% if show_delete_link %}
    {% url opts|admin_urlname:'delete' original.pk|admin_urlquote as delete_url %}
    <p class="deletelink-box"><a href="{% add_preserved_filters delete_url %}" class="deletelink">{% trans "Delete" %}</a></p>
{% endif %}
{% if show_save_as_new %}<input class="myclass" type="submit" value="{% trans 'Save as new' %}" name="_saveasnew">{% endif %}
{% if show_save_and_add_another %}<input class="myclass" type="submit" value="{% trans 'Save and add another' %}" name="_addanother">{% endif %}
{% if show_save_and_continue %}<input class="myclass" type="submit" value="{% if can_change %}{% trans 'Save and continue editing' %}{% else %}{% trans 'Save and view' %}{% endif %}" name="_continue">{% endif %}
{% if show_close %}<a href="{% url opts|admin_urlname:'changelist' %}" class="closelink">{% trans 'Close' %}</a>{% endif %}
{% endblock %}
</div>

But, I don’t see any changes (class="myclass" there aren’t).

My next changes will be the js code to disable all buttons at on click… but now I’m blocked on this first problem.

I use python 3 and Django 2

Asked By: Safari

||

Answers:

If you are trying to add classes to the inputs just so you can use JavaScript to disable them upon click, that’s not necessary. You should be able to select the buttons pretty easily without adding classes to them. For example, using jQuery included in the Django admin:

django.jQuery(".submit-row :submit").attr("disabled", "disabled")

If you truly need to override the rendering of the button HTML, you should look at overriding the blocks submit_buttons_bottom and submit_buttons_top:

{% extends "admin/change_form.html" %}
{% if save_on_top %}your submit buttons here{% booking_submit_row %}{% endblock %}{% endif %}
{% block submit_buttons_bottom %}your submit buttons here{% endblock %}

If you want to avoid repeating yourself with this approach, consider using a custom submit-line.html and building a custom templatetag to output your submit buttons in the above template, like so:

from django.contrib.admin.templatetags.admin_modify import submit_row
from django.template import Library


register = Library()

@register.inclusion_tag("admin/<your app>/<your model>/submit_line.html", takes_context=True)
def custom_submit_row(context):
    ctx = submit_row(context)
    original = context["original"] if "original" in context else None
    # adjust as you need based on your context
    return ctx

Now in your custom change_form.html you can use:

{% extends "admin/change_form.html" %}
{% load custom_admin_modify %}
{% if save_on_top %}{% custom_submit_row %}{% booking_submit_row %}{% endblock %}{% endif %}
{% block submit_buttons_bottom %}{% custom_submit_row %}{% endblock %}

Hope that helps – good luck!

Answered By: lukewarm

I could remove "SAVE" button, "Save and continue editing" button, "Save and add another" button and "Delete" button from "Change person" page with this code below for "admin.py". But, when I removed "SAVE" button, "Close" button appeared then I don’t know how to remove "Close" button:

# "admin.py"

from django.contrib import admin
from .models import Person

@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
    def changeform_view(self, request, object_id=None, form_url='', extra_context=None):
        extra_context = extra_context or {}

        extra_context['show_save'] = False
        extra_context['show_save_and_continue'] = False

        return super().changeform_view(request, object_id, form_url, extra_context)

    def has_add_permission(self, request, obj=None):
        return False

    def has_delete_permission(self, request, obj=None): # Here
        return False

Then, "Delete" button is removed from "Change person" page as shown below:

enter image description here

Answered By: Kai – Kazuya Ito