Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom button to django admin panel

I want to add button to admin panel to my model, I have overwrite template (path: templetes/admin/myapp/mymodel/change_list.html)

change_list.html

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

{% block result_list %}
<div class="object-tools">
    <a href="{% url 'myurl' %}" class="btn btn-high btn-success">Import</a>
</div>
{{ block.super }}
{% endblock %}

In admin.py

class ImportAdmin(admin.ModelAdmin):
    change_list_template = 'admin/myapp/mymodel/change_list.html'

But I can not see the button.

like image 895
git-e Avatar asked Nov 23 '16 09:11

git-e


People also ask

Can we customize Django admin panel?

You can fully customize the admin by changing the templates used to render pages. The Django template engine has a defined order for loading templates. When it loads a template, it uses the first template that matches the name. You can override admin templates by using the same directory structure and file names.

How do I customize my Django dashboard?

To customize any default page we need to create our own template directory, create the file using the same name and position in the parent directory and inform Django to use it. To go deeper, we will customize the 404 error page and configure Django to use it.

Can we change Django admin template?

To do so, you will have to change the project's settings.py . Find the TEMPLATES section and modify accordingly. To override the default template you first need to access the template you want to modify from the django/contrib/admin/templates/admin directory.


3 Answers

Another option for adding a button would be to use django-object-actions.

First, install it: pip install django-object-actions. (Also add django-object-actions to your requirements file if you have one).

Second, add django_object_actions to your INSTALLED_APPS.

You can then use it in your admin.py like so:

from django.contrib import admin
from django_object_actions import DjangoObjectActions

class ImportAdmin(DjangoObjectActions, admin.ModelAdmin):
    def imports(modeladmin, request, queryset):
        print("Imports button pushed")

    changelist_actions = ('imports', )

You should now see an Imports button in the admin, and when pressed, the imports function defined in ImportAdmin will be called.

For more information please refer to: https://github.com/crccheck/django-object-actions.

like image 52
t-payne Avatar answered Nov 05 '22 09:11

t-payne


It works as below ("Import" button right side).

enter image description here

Django = 1.11

admin/change_list.html: Add the URL with "admin:". Otherwise, it will not resolve the URL.

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

{% block object-tools-items %}
{{ block.super }}
<li>
    <a href="{% url 'admin:myurl' %}" class="btn btn-high btn-success">Import</a>
</li>
{% endblock %}

admin.py: Add the custom template URL

class ImportAdmin(admin.ModelAdmin):
    change_list_template = 'admin/myapp/mymodel/change_list.html'

Django >1.8

settings.py: TEMPLATE_LOADERS deprecated. Set TEMPLATES as below.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': False,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
            'loaders': [
                'admin_tools.template_loaders.Loader',
                ('django.template.loaders.cached.Loader', [
                    'django.template.loaders.filesystem.Loader',
                    'django.template.loaders.app_directories.Loader',
                ]),
            ],

        },
    },
]
like image 30
Isanka Wijerathne Avatar answered Nov 05 '22 08:11

Isanka Wijerathne


You will be able to see the button next to Add button at the top of list page with the following content.

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

{% block object-tools-items %}
    {{ block.super }}
    <li>
        <a href="{% url 'myurl' %}" class="btn btn-high btn-success">Import</a>
    </li>
{% endblock %}
like image 22
arulmr Avatar answered Nov 05 '22 08:11

arulmr