31👍
[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.
7👍
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:
-
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. -
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 %}
-
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).
- Django i18n: how to not translate the admin site?
- Django static files on heroku
- Can't login to Django /admin interface
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.