I have an application in Django with a routine which would be available only to the admin. What I want to do is add a button to perform the routine in this application's section of the admin app.
Am I supposed to make a template for it, and if that's the case, how do I add a html template for an app in the admin. Or maybe there's a command to simply add a button?
Messing with the admin forms can be complicated but i've commonly found that adding links, buttons, or extra info is easy and helpful. (Like a list of links to related objects witout making an inline, esp for things that are more viewed than edited).
From Django docs
Because of the modular design of the admin templates, it is usually neither necessary nor advisable to replace an entire template. It is almost always better to override only the section of the template which you need to change.
This will add a list over the top of the form.
Place in templates/admin/[your_app]/[template_to_override]
:
{% extends "admin/change_form.html" %} {% block form_top %} {% for item in original.items %} {{ item }} {% endfor %} {% endblock %}
Django1.10:
1) Override admin/submit_line.html
:
{% load i18n admin_urls %} <div class="submit-row"> {% if extra_buttons %} {% for button in extra_buttons %} {{ button }} {% endfor %} {% endif %} {% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_save" />{% endif %} {% if show_delete_link %} {% url opts|admin_urlname:'delete' original.pk|admin_urlquote as delete_url %} <p class="deletelink-box"><a href="{% add_preserved_filters delete_url %}" class="deletelink">{% trans "Delete" %}</a></p> {% endif %} {% if show_save_as_new %}<input type="submit" value="{% trans 'Save as new' %}" name="_saveasnew" />{% endif %} {% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother" />{% endif %} {% if show_save_and_continue %}<input type="submit" value="{% trans 'Save and continue editing' %}" name="_continue" />{% endif %} </div>
This assumes, of course, that button
's string representation is an appropriate browser input
or button
element, and is marked safe with django.utils.safestring.mark_safe
. Alternatively, you could use the safe
template filter or access the attributes of button
directly to construct the <input>
. In my opinion, it's better to isolate such things to the python level.
2) Override MyModelAdmin.change_view
:
def change_view(self, request, object_id, form_url='', extra_context=None): extra_context = extra_context or self.extra_context() return super(PollAdmin, self).change_view( request, object_id, form_url, extra_context=extra_context, )
This method enables you to add buttons to any ModelAdmin
easily. Alternatively to step (1), you could extend admin/change_form.html
and override block submit_row
. This would be slightly more verbose due to extra tags required in the template.
If you want the extra action available across all of your models (or a specific subset) then subclass ModelAdmin
with the desired functionality (an example would be to add archiving to your models. You could even add an override for delete--and the other default buttons--so that the mode is archived instead of deleted; this would require some template modifications)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With