Add custom button to django admin panel

时光总嘲笑我的痴心妄想 提交于 2021-02-04 12:36:05

问题


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.


回答1:


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

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',
                ]),
            ],

        },
    },
]



回答2:


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 %}



回答3:


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 import(modeladmin, request, queryset):
        print("Import button pushed")

    changelist_actions = ('import', )

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

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



来源:https://stackoverflow.com/questions/40760880/add-custom-button-to-django-admin-panel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!