A while ago (well, actually a long time ago since I had this blog post laying around as a draft, until I stumbled across a stack overflow question that this post would answer) I wanted to customize Django’s admin app, by adding a link to a statistics page that I had built, on the index.html page of the admin app.

I could have copied the index.html from the templates directory in the admin app, to my project’s templates directory and added my link, but this would have given me a maintenance task to keep Django’s admin templates up to date in future versions. So what I really wanted to do was to define my own admin/index.html file in the project’s templates directory, and then in my index template extend the index template from the admin app. However this isn’t supported by Django be default.

When I searched the web I found all sort of solutions to the problem, some involving symlinking the admin’s index.html to another path and then extending the symlink, but that wasn’t an option for me since I develop on both Windows and Unix, and besides, it didn’t feel like a very clean solution.

Finally I found very nice template loader on djangosnippets.org which solved my problem! It allows you to extend a template from a specific Django app by using the following syntax:

{% extends "admin:admin/index.html" %}

As you can see, I specify the app where the template resides before the colon. The admin/index.html, in my project’s template directory, that I finally ended up with looks like this:

{% extends "admin:admin/index.html" %}

{% block sidebar %}
    {{block.super}}
    <div>
        < h1>Statistics< /h1>
        < a href="/admin/station/stats/">Published Stations< /a>
    </div>
{% endblock %}

Now I won’t have to change anything if the Django admin app’s index.html is updated in a newer version of Django, unless the sidebar block is removed, or something similar.

Also, if you decide to use this trick, don’t forget to add the template loader to the TEMPLATE_LOADERS list in your project’s settings.py file.