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 %}
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)