Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 421
Peters Avatar asked Jan 23 '13 16:01

Peters


2 Answers

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
like image 114
Kurt Avatar answered Nov 15 '22 09:11

Kurt


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.

like image 28
Adam Taylor Avatar answered Nov 15 '22 07:11

Adam Taylor