14
I know it’s late, but…
If extending – which is a far better option than duplicating – the key is to have it named anything except /admin/change_form.html
.
(Although the OP referred to template/admin/change_form.html
, this is simply because a path in his TEMPLATE_DIRS tuple ends in ‘/template’ – mine generally end in ‘/templates’ – but, these directories can be named anything and located anywhere.)
It will be used automatically on a per-app basis if named /admin/<MyAppName>/change_form.html
It will be used automatically on a per-model basis if named /admin/<MyAppName>/<MyModelName>/change_form.html
It can be named anything if specified explicitly in the ModelAdmin
class MyModelAdmin(admin.ModelAdmin):
change_form_template = 'subdir/my_change_form.html'
Finally, if insistent on naming it /admin/change_form.html
, you can – provided that the extends
tag contains the full path to your django installation instead of a relative one.
4
You are in admin/change_form.html
and you extend admin/change_form.html
. You cannot extend the same template which you are in.
You probably expected that if you override template from admin application, you can extend the one you override. But this is not how it works. When you override a template you cannot access it.
Solution to your problem is to copy original template and change things you don’t like.
- Duplicate Django Model Instance and All Foreign Keys Pointing to It
- How to setup APScheduler in a Django project?
- Determine if an attribute is a `DeferredAttribute` in django
- Token Authentication Django Rest Framework HTTPie
- How to update a cookie in Django
3
Also, you can point your AdminOptions
class to another template using the change_form_template
property.
Something like:
class MyOptions(AdminOptions):
change_form_template = 'myapp/my_change_form.html'
And myapp/my_change_form.html
:
{% extends "admin/change_form.html" %}
- How can I specify the parameter for POST requests while using APIView with django-rest-swagger
- Django CSRF token won't show
2
I had the same problem. Solved by placing the overridden template under myapp/templates/admin/myapp instead of myapp/templates/admin.
1
The best way to do this that I have found is to use '..'
to go up a couple of directories, then go back down into directories that should only be found in the Django code base.
As the Django templates are in something like "django/contrib/admin/templates/admin"
, I found that this worked me:
{% extends "../../admin/templates/admin/change_form.html" %}
If that still causes a clash with some other structure you have, you could go further:
{% extends "../../../contrib/admin/templates/admin/change_form.html" %}
or even:
{% extends "../../../../django/contrib/admin/templates/admin/change_form.html" %}
Although it is a little hacky, at least by doing the above you don’t have to use some other technique that involves copying the django source or setting up a symlink.
- Django-mptt get_descendants for a list of nodes
- Django: how to filter() after distinct()
- Using MongoDB as our master database, should I use a separate graph database to implement relationships between entities?
0
With Django core it’s impossible. But it is not impossible.
Copy and paste “the original template and change things you don’t like” it’s very ugly.
Don’t make in the templates, whatever you don’t make in python
This solution is to any template:
- How can I make SSE with Python (Django)?
- Python/Django development, windows or linux?
- Django – http code 304, how to workaround in the testserver?