Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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: enter image description here

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).

like image 242
A.J. Avatar asked Oct 23 '13 12:10

A.J.


People also ask

Can we customize Django admin panel?

The Django admin is a powerful built-in tool giving you the ability to create, update, and delete objects in your database using a web interface. You can customize the Django admin to do almost anything you want.

How can I remove extra's from Django admin panel?

You can add another class called Meta in your model to specify plural display name. For example, if the model's name is Category , the admin displays Categorys , but by adding the Meta class, we can change it to Categories . Save this answer.


3 Answers

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).

like image 93
jnns Avatar answered Nov 15 '22 21:11

jnns


[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.

like image 21
ytyng Avatar answered Nov 15 '22 23:11

ytyng


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.

like image 28
Alex Parakhnevich Avatar answered Nov 15 '22 21:11

Alex Parakhnevich