Peters
Peters

Reputation: 123

How to add top save button on django admin change list?

I want to have a save button on top of django admin change list page. It seems that django don't have this functionality built-in. The save_on_top option only controls behavior on change form page. Any suggestion is welcome.

Upvotes: 7

Views: 8140

Answers (3)

Kurt
Kurt

Reputation: 2403

In Django 3 (and maybe earlier, not sure) in your custom admin form add save_on_top = True

class MyAdmin(admin.ModelAdmin):
    save_on_top = True

Upvotes: 9

frnhr
frnhr

Reputation: 12903

If you don't mind having pagination links at the top of the page as well, you can do it with a few lines of template code, worksforme in Django 2.0.

Create my_app/templates/admin/my_app/my_model/change_list.html:

{% extends "admin/change_list.html" %}
{% load admin_list %}

{% block result_list %}
    {% pagination cl %}
    {{ block.super }}
{% endblock %}

This will render the pagination and the save button:

enter image description here

Could benefit from a line or two of CSS, though...

Upvotes: 3

Adam Taylor
Adam Taylor

Reputation: 4839

First, you need a way to extend the template found at django/contrib/admin/templates/admin/change_list.html. If you don't already know how to do that, check out this answer and this answer.

Next, you need to create your own change_list.html template and put code similar to the following in it. For the sake of simplicity, I've included inline CSS. However, this is bad practice, so you should not do it. Assuming you move the CSS to an external file, you won't need to load admin_static. Lastly, the extends line that you use might not be exactly the same as what I've shown here.

{% extends "contrib/admin/templates/admin/change_list.html" %}
{% load i18n admin_static %}

{% block result_list %}
    {% if cl.formset and cl.result_count %}
        <div style="border-bottom: 1px solid #ccc; background: white url({% static "admin/img/nav-bg.gif" %}) 0 180% repeat-x; overflow: hidden;">
            <p>
                <input type="submit" name="_save" class="default" value="{% trans 'Save' %}"/>
            </p>
        </div>
    {% endif %}

    {{ block.super }}
{% endblock %}

The {% if %} tag and the <input> tag inside of it is from django/contrib/admin/templates/admin/pagination.html.

The CSS is based on the CSS for #changelist .paginator and is found in django/contrib/admin/static/admin/css/changelists.css.

Upvotes: 6

Related Questions