Overridding Django Admin's object-tools bar for one Model
Asked Answered
B

3

17

I am looking for the solution. I think a lot of different examples have confused me a little. I want to override the object-tools template in the Django admin.

I have one button on my model's tools:

picture of django admin containing a button with the label "add customer"

I need one other same type of button "Upload file". This should redirect me to the another view which I want to design myself in the same admin base template (with just one upload file control).

Bangor answered 23/10, 2013 at 12:54 Comment(0)
F
34

[A]

Move "django.contrib.admin" in your INSTALLED_APPS to end of INSTALLED_APPS.

You can make template file

<your_app>/templates/admin/<your_app>/<your_model>/change_list.html

source code:

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

{% block object-tools-items %}
  <li>
    <a href="<your-action-url>" class="addlink">
     Upload file
    </a>
  </li>
  {{ block.super }}
{% endblock %}

[B]

Add change_list_template into your ModelAdmin.

class MyModelAdmin(admin.ModelAdmin):

    change_list_template = '<path-to-my-template>.html'

And write template like [A] source code.

Felicidad answered 30/8, 2017 at 11:0 Comment(3)
See related question #45677000Navicert
You should mention {{block.super}} as well, It would be helpful for new users to get the admin/change_form_object_tools.html :-)Shetler
what if it is the built-in User, not <your_model>?Bettiebettina
G
8

It has become lot easier to accomplish what you're looking for since this question has been asked the first time.

The Django documentation has a section on how to override admin templates. To add a button to the changelist object tools, follow these steps:

  1. Copy Django's version of the change_list_object_tools.html template file into your project's or app's template folder: templates/admin/<app>/<model>/change_list_object_tools.html.

    You can obtain the file form your virtual env's site-packages folder:

    cp $VIRTUAL_ENV/lib/python3.8/site-packages/django/contrib/admin/templates/admin/change_list_tools.html templates/admin/$APP/$MODEL/ 
    

    Note that you might need to adjust the path to site-packages for your Python version.

  2. Now open the template file. It will look liek this:

    {% load i18n admin_urls %}
    
    {% block object-tools-items %}
      {% if has_add_permission %}
      <li>
        {% url cl.opts|admin_urlname:'add' as add_url %}
        <a href="{% add_preserved_filters add_url is_popup to_field %}" class="addlink">
          {% blocktrans with cl.opts.verbose_name as name %}Add {{ name }}{% endblocktrans %}
        </a>
      </li>
      {% endif %}
    {% endblock %}
    
  3. Add the link to your custom view:

      …
      {% if has_add_permission %}
      <li><a class="addlink" href="{% url 'upload-file' %}">Upload file</a></li>
      <li>
      …
    

That's it! If you didn't know: you can add custom views and URLs to a ModelAdmin using ModelAdmin.get_urls() (docs). To not have to hardcode your custom admin URLs, you can of course reverse them (docs).

Getty answered 13/11, 2020 at 14:29 Comment(2)
What of change or updateAshleyashli
@LutaayaHuzaifahIdris Could you rephrase your question? I don't understand what you're asking.Getty
D
1

You can try to use https://github.com/texastribune/django-object-actions.

This will allow you to add buttons with custom logic.
Though, I'm not sure whether you'll be able to a buttom for your list screen, as I did it only for a model-edit page.

Decosta answered 23/10, 2013 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.