alejoss alejoss - 4 months ago 32
Python Question

Django. Customize action for model

I need to edit the "Add Object" action in the admin. It needs to redirect the user to a custom cause the logic required for adding objects is too complex to be managed in the admin. So, how do I make this possible? i.e:

enter image description here

The picture shows a django-suit admin, but the question is the same. How can I make that button redirect to a custom url? Or, how can I create a similar button that redirects to a custom url (I could disable the default create and leave only the custom button).


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 %}
          <a href="your/custom/url" class="addlink">
            {% blocktrans with cl.opts.verbose_name as name %}Add {{ name }}{% endblocktrans %}
        {% 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.


Add above dir in TEMPLATE DIRS paths.