In Django, you can override the Django Admin change forms by providing a path to a new template:
@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
# some other admin form config...
# override change form template...
change_form_template = "admin/some_custom_change_form.html"
Then you can create a template within your application at app/templates/admin/some_custom_change_form.html
that needs to extend the original template:
{% extends "admin/change_form.html" %}
<!-- Add your changes here -->
<p>Hello World!</p>
I am trying to override not only the change form, but the form for creation/addition too.
Django also provides add_form_template
for the ModelAdmin interface too:
@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
# some other admin form config...
# override add and change form template...
change_form_template = "admin/some_custom_change_form.html"
add_form_template = "admin/some_custom_add_form.html"
Now I just need the original template to override.
I can't for the life of me find in documentation where the templates you can override are located, so I made a best guess and tried admin/add_form.html
{% extends "admin/add_form.html" %}
<!-- Add your changes here -->
<p>Hello World!</p>
Which throws an Exception on render of the add page, as it cannot find the chosen template to extend it.
I've also tried using {% extends "admin/change_form.html" %}
but that doesn't work as a different template is being used in the back end, so my changes don't show. I've also tried pointing at my template that is known to work for the change form, but still doesn't work.
Any ideas?
Even better if someone can point me to the page in the documentation that gives these template locations, as I can't find it anywhere.
I managed to find the location of the templates.
django/contrib/admin/templates/admin
Within here, I can see that there is no form for add
but in fact the change_form.html
is the one used for the add form too, just with some conditionals in the definition for when the form is rendering an add or change form.
My mistake was trying to override block that is only available in the change form, not the add form. I was overriding:
{% block object-tools-items %}
Other blocks can be overridden instead.