How do I override an admin template (e.g. admin/index.html) while at the same time extending it (see https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-vs-replacing-an-admin-template)?
First - I know that this question has been asked and answered before (see Django: Overriding AND extending an app template) but as the answer says it isn't directly applicable if you're using the app_directories template loader (which is most of the time).
My current workaround is to make copies and extend from them instead of extending directly from the admin templates. This works great but it's really confusing and adds extra work when the admin templates change.
It could think of some custom extend-tag for the templates but I don't want to reinvent the wheel if there already exists a solution.
On a side note: Does anybody know if this problem will be addressed by Django itself?
This site had a simple solution that worked with my Django 1.7 configuration.
FIRST: Make a symlink named admin_src in your project's template/ directory to your installed Django templates. For me on Dreamhost using a virtualenv, my "source" Django admin templates were in:
SECOND: Create an admin directory in templates/
So my project's template/ directory now looked like this:
THIRD: In your new template/admin/ directory create a base.html file with this content:
FOURTH: Add your admin favicon-admin.ico into your static root img folder.
Done. Easy.
Update:
Read the Docs for your version of Django. e.g.
https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#admin-overriding-templates https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#admin-overriding-templates
Original answer from 2011:
I had the same issue about a year and a half ago and I found a nice template loader on djangosnippets.org that makes this easy. It allows you to extend a template in a specific app, giving you the ability to create your own admin/index.html that extends the admin/index.html template from the admin app. Like this:
I've given a full example on how to use this template loader in a blog post on my website.
With
django
1.5 (at least) you can define the template you want to use for a particularmodeladmin
see https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#custom-template-options
You can do something like
With
change_form.html
being a simple html template extendingadmin/change_form.html
(or not if you want to do it from scratch)I agree with Chris Pratt. But I think it's better to create the symlink to original Django folder where the admin templates place in:
and as you can see it depends on python version and the folder where the Django installed. So in future or on a production server you might need to change the path.
The best way to do it is to put the Django admin templates inside your project. So your templates would be in
templates/admin
while the stock Django admin templates would be in saytemplate/django_admin
. Then, you can do something like the following:templates/admin/change_form.html
If you're worried about keeping the stock templates up to date, you can include them with svn externals or similar.
You can use django-overextends, which provides circular template inheritance for Django.
It comes from the Mezzanine CMS, from where Stephen extracted it into a standalone Django extension.
More infos you find in "Overriding vs Extending Templates" (http:/mezzanine.jupo.org/docs/content-architecture.html#overriding-vs-extending-templates) inside the Mezzanine docs.
For deeper insides look at Stephens Blog "Circular Template Inheritance for Django" (http:/blog.jupo.org/2012/05/17/circular-template-inheritance-for-django).
And in Google Groups the discussion (https:/groups.google.com/forum/#!topic/mezzanine-users/sUydcf_IZkQ) which started the development of this feature.
Note:
I don't have the reputation to add more than 2 links. But I think the links provide interesting background information. So I just left out a slash after "http(s):". Maybe someone with better reputation can repair the links and remove this note.