3

enter image description here

I'm new to Django!

I'm using Django Admin. How can I make a new button(near save,...) and post the information and use it in a python script(I am using Django version 2).

admin.py:

admin.site.register(Router)
admin.site.register(Peer, PeerModelAdmin)
admin.site.register(Prefixe, PrefixModelAdmin)
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Eteq Eteq
  • 73
  • 1
  • 1
  • 6
  • You can check the docs [here](https://docs.djangoproject.com/en/2.1/ref/contrib/admin/actions/). It's well explained. Also there's many posts like [this](https://medium.com/@hakibenita/how-to-add-custom-action-buttons-to-django-admin-8d266f5b0d41). Also duplicated with [this question](https://stackoverflow.com/questions/723421/custom-actions-in-django-admin) – seuling Dec 04 '18 at 15:32

2 Answers2

2

You need to override the change_form_template. Try like this:

class YourModelAdmin(admin.ModelAdmin):
    change_form_template = 'custom_change_form.html'

In custom_change_form.html it should be extended from admin/change_form.html and it can be like this:

{% load i18n %}
{% extends 'admin/change_form.html' %}
    <button> Your Custom Button </button>
    <input type="submit" value="{% trans 'Save' %}" class="default" name="_save">
{% endblock %}
ruddra
  • 50,746
  • 7
  • 78
  • 101
  • thanks do I need to make directory templates/admin/change_form.html? – Eteq Eteq Dec 04 '18 at 16:47
  • no, it should automatically import it from django library. – ruddra Dec 04 '18 at 16:56
  • I get this error: Could not parse the remainder: '“admin/change_form.html”' from '“admin/change_form.html”' – Eteq Eteq Dec 05 '18 at 08:23
  • You have used single quote and double quote together in `'“admin/change_form.html”' `, instead use `'admin/change_form.html' ` like described in the answer – ruddra Dec 05 '18 at 08:31
  • Thanks! still got an error: Invalid block tag on line 4: 'submit_row', expected 'endblock'. Did you forget to register or load this tag? – Eteq Eteq Dec 05 '18 at 08:39
  • Well, I am not sure. Maybe you can try my updated answer. hope it helps – ruddra Dec 05 '18 at 08:44
  • still the same error: Exception Location: /usr/local/lib/python3.6/dist-packages/django/template/base.py in invalid_block_tag, line 534 – Eteq Eteq Dec 05 '18 at 09:38
  • 1
    Invalid block tag on line 3: 'trans'. Did you forget to register or load this tag – Eteq Eteq Dec 05 '18 at 10:17
  • Oh, you need to load `i18n`. please see my updated answer :( – ruddra Dec 05 '18 at 10:19
  • 3
    this works: {% extends 'admin/change_form.html' %} {% block submit_buttons_bottom %} {{ block.super }}
    {% endblock %}
    – Eteq Eteq Dec 05 '18 at 10:21
1

You can add a custom button to "Add" form and "Change" form for a specifc admin.

First, about how to add a custom button to "Add" form and "Change" form for a specifc admin, see How to add a custom button at the bottom of "Add" form and "Change" form for a specifc admin or How to add a custom button right next to "SAVE" button on "Add" form and "Change" form for a specifc admin

Next, set "response_add()" for "Add" form and "response_change()" for "Change" form in "class PersonAdmin(admin.ModelAdmin):" to define the action after pressing a custom button as shown below. *Whether or not setting "response_add()" and "response_change()", inputted data to fields is saved after pressing a custom button:

# "admin.py"

from django.contrib import admin
from .models import Person

@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
    change_form_template = "admin/custom_change_form.html"
    
    def changeform_view(self, request, object_id=None, form_url='', extra_context=None):
        extra_context = extra_context or {}
        
        extra_context['custom_button'] = True
        
        return super().changeform_view(request, object_id, form_url, extra_context)

    def response_add(self, request, obj, post_url_continue=None):

        if "_custom_button" in request.POST:
            # Do something
            return super().response_add(request, obj, post_url_continue)
        else:
            # Do something
            return super().response_add(request, obj, post_url_continue)

    def response_change(self, request, obj):
        
        if "_custom_button" in request.POST:
            # Do something
            return super().response_change(request, obj)
        else:
            # Do something
            return super().response_change(request, obj)
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129