Add custom button near Save button Django-admin

Question:

I have added a custom button in django admin, however its below the Save and Save and Close buttons, how can I make this custom button on the same line with the 2 buttons above, below is the image:

enter image description here

Then, how I overridden the button with the templates:

{% extends 'admin/custominlines/change_form.html' %}
{% load i18n %}
 {% block submit_buttons_bottom %}
     {{ block.super }}
       {% if request.GET.edit %}
           <div class="submit-row">
           {% for obj in transitions %}
                <input type="submit" value="{{ obj }}" name="{{ obj }}">
           {% endfor %}
            </div>
     {% endif %}
{% endblock %}

Answers:

Django has a <div class="submit-row"> for each row. You are adding a new row. What you need to do is to put your buttons in the same div with Django. Here is Django’s code modified to have your buttons.

{% 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 and original %}
    {% 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 type="submit" value="{% trans 'Save as new' %}" name="_saveasnew">{% endif %}
{% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother">{% endif %}
{% if show_save_and_continue %}<input 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 %}
// Django code above

// Your buttons below
{% for obj in transitions %}
    <input type="submit" value="{{ obj }}" name="{{ obj }}">
{% endfor %}

</div>

Your code at the end will look like this.

{% extends 'admin/custominlines/change_form.html' %}
{% load i18n %}
{% block submit_buttons_bottom %}
   <div class="submit-row">

{% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_save">{% endif %}
{% if show_delete_link and original %}
    {% 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 type="submit" value="{% trans 'Save as new' %}" name="_saveasnew">{% endif %}
{% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother">{% endif %}
{% if show_save_and_continue %}<input 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 %}

// Django code above

// Your buttons below

{% for obj in transitions %}
    <input type="submit" value="{{ obj }}" name="{{ obj }}">
{% endfor %}

</div>     
{% endblock %}
Answered By: Keyvan Khademi

You don’t have to override change_form.html. Instead, you can just override submit_line.html

{% extends 'admin/submit_line.html' %}
{% load i18n admin_urls %}

{% block submit-row %}
    {{ block.super }}
    {% for obj in transitions %}
        <input type="submit" value="{{ obj }}" name="{{ obj }}">
    {% endfor %}
{% endblock %}
Answered By: Dharanidhar Reddy

You can add a custom button in the same line of "SAVE" button on "Add" form and "Change" form for a specifc admin.

To do that, by creating "templates/admin/custom_change_form.html" and "templates/admin/submit_line.html" in the root django project directory, you need to override "change_form.html" under django library whose path is "django/contrib/admin/templates/admin/change_form.html" and "submit_line.html" under django library whose path is "django/contrib/admin/templates/admin/submit_line.html".

Then, you need to add the code of a custom button to "submit_line.html" as shown below:

# "submit_line.html"

{% load i18n admin_urls %}
<div class="submit-row">
{% block submit-row %}
{% if custom_button %}<input type="submit" style="float: right;margin-left: 8px;" value="{% translate 'Custom button' %}" name="_custom_button">{% endif %}
{% if show_save %}<input type="submit" value="{% translate 'Save' %}" class="default" name="_save">{% endif %}
{% if show_delete_link and original %}
    {% 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">{% translate "Delete" %}</a></p>
{% endif %}
{% if show_save_as_new %}<input type="submit" value="{% translate 'Save as new' %}" name="_saveasnew">{% endif %}
{% if show_save_and_add_another %}<input type="submit" value="{% translate 'Save and add another' %}" name="_addanother">{% endif %}
{% if show_save_and_continue %}<input type="submit" value="{% if can_change %}{% translate 'Save and continue editing' %}{% else %}{% translate 'Save and view' %}{% endif %}" name="_continue">{% endif %}
{% if show_close %}<a href="{% url opts|admin_urlname:'changelist' %}" class="closelink">{% translate 'Close' %}</a>{% endif %}
{% endblock %}
</div>

You can find more detail in How to add a custom button right next to "SAVE" button on "Add" form and "Change" form for a specifc admin then you will archive what you want to do.

Answered By: Kai – Kazuya Ito