[Fixed]-Django. Customize action for model

1👍

Override change_list_template html, the block object-tools-items. It’s where add button is placed.

class MyModelAdmin(admin.ModelAdmin):
    change_list_template = 'change_list.html'

In your change_list.html

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

{% block object-tools-items %}
        {% if has_add_permission %}
        <li>
          <a href="your/custom/url" class="addlink">
            {% blocktrans with cl.opts.verbose_name as name %}Add {{ name }}{% endblocktrans %}
              </a>
        </li>
        {% endif %}
{% endblock %}

You need to add your new html in any dir that is included on TEMPLATE_DIRS options. But, you should do it inside your model’s app.

-app
    -templates
         -admin
              change_list.html

Add above dir in TEMPLATE DIRS paths.

👤levi

Leave a comment