Code

This page is out of date. Please see https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-admin-templates for docs on overriding admin templates in the latest versions of Django.

Extending the Admin Templates

Intro

Django's admin stuff is great. Often, you would like to just add just a little and you would be all set. For example it would be nice to be able to add links to related data on a change form (detail). With 1 little hack, this is easy. The hack is that you need to copy (if you are on Windows, link on Nixes) the admin template that you want to extend so that the template loader does not get confused.

(Of course, there is probably some simpler mechanism I have not come across or recognized yet, but... live and learn.)

Steps

  1. make a directory in your template path named admin_copies
  2. copy change_form.html from your Django install's contrib/admin/templates/admin
  3. create the template to override admin, in this case admin/auction/participant/change_form.html (the template loader find it because it looks first in your template_dirs before looking in the app directories, i.e. django.contrib.admin.)
  4. your change_form.html will look something like:
    {% extends "admin_copies/change_form.html" %}
    {% block coltype %}colMS{% endblock %}
    {% block sidebar %}
    <div id="content-related">
        <div id="navcontainer">
            <ul id="navlist">
                <li><a href="../merge/">Merge {{ form.data.id }} to another</a></li>
                {% ifequal form.data.is_shareholder 1 %}
                <li><a href="/admin/manager/shareholders/{{ form.data.id }}/">Show shareholder data for {{ form.data.id }}</a></li>
                {% endifequal %}
            </ul>
        </div>
    </div>
    {% endblock %}
    
    This makes the layout use a sidebar and populates it with some links for related functions or data.

Conclusion

You need the admin_copies directory because you can not extend admin/change_form since it will never be found--you are editing the very file that overrides it. So you give the template loader another place to look. Hence, the copy.

The above template--with some CSS--produces:

All very neat and clean.

Last modified 19 months ago Last modified on 09/25/12 15:34:13

Attachments (1)

Download all attachments as: .zip