Previous Page Next Page

Hour 18. Customizing the Admin Interface

What You'll Learn in This Hour

  • How to override admin templates at the site level

  • How to create your own custom admin views

  • How to override admin templates at the application level

  • How to override admin templates at the object level

  • How to override admin templates at the block level

  • How to override the built-in admin views

The preceding hour discussed how to customize the look of models in the admin interface. This hour discusses how to customize the look, feel, and behavior of the admin interface.

The admin interface is made up of URL patterns that link to view functions that render template files. The URL patterns, views, and templates aren't any different from what you have used so far. They include a large amount of code and CSS formatting, but they are still just URL patterns, view functions, and templates.

The admin templates are stored in the following location relative to the root of the Django installation:

django/contrib/admin/templates/admin/

The admin URL patterns are defined in the following URLconf file relative to the root of the Django installation:

django/contrib/admin/urls.py

The admin views are located in several Python modules in the following location relative to the root of the Django installation:

django/contrib/admin/views/

This hour discusses some of the ways you can modify the look and feel of the admin interface by creating your own custom admin code to either add or override existing admin views.

By the Way

The information in this hour is not meant to be inclusive. It barely scratches the surface. It is designed to give you some starting points for customizing the admin interface. There is just so much you can do in so many ways. The best documentation available for understanding how to customize the admin interface is the code itself.


Overriding Admin Templates at the Site Level

The most common type of customization that you will likely want to perform in the admin interface is to alter or replace the admin HTML template files. This can present a problem.

If you modify the admin templates in the Django installation, you customize the admin interface. The problem is that you customize the admin interface for all sites on that installation, and if you update Django, your customizations are overwritten.

The Django template loaders solve this problem. If you have added a directory to the TEMPLATE_DIRS setting in the site's settings.py file, the template loaders look there first to load template files. You can override any admin template at the site level by creating a template there using the same name.

By the Way

Instead of creating your own custom templates, you can copy the admin template from the Django installation to your own template admin directory and then make the modifications you need to make.


For example, if you wanted to override the base_site.html admin template, you could just create a file named admin/base_site.html in your template directory.

The following are the most common admin templates that you might want to override:

Try It Yourself: Override the base_site.html Template

In this section, you will override the base_site.html template to add the iFriends name to the window title and banner for the admin interface. Follow these steps to make the change:

1.
Create a directory called iFriends/templates/admin. Any admin templates in this directory are loaded before the ones in the Django installation, because you have already added this directory to your TEMPLATE_DIRS setting.

2.
Copy the following file from the Django installation to the admin directory you just created:

django/contrib/admin/templates/admin/base_site.html

3.
Open the iFriends/templates/base_site.html file in an editor.

4.
Modify the following lines of code, shown in Listing 18.1, to add the iFriends name to the window title and banner in the admin interface:

{% block title %}{{ title|escape }} | {% trans 'iFriends site admin' %}
  {% endblock %}
. . .
<h1 id="site-name">{% trans 'iFriends administration' %}</h1>

5.
Save the iFriends/templates/base_site.html file.

6.
Go to the following URL in a browser to see the changes shown in Figure 18.1:

http://127.0.0.1:8000/admin/

Figure 18.1. The admin interface showing the modified window and banner titles.


The changes show up on all admin pages that extend the base_site.html template.

Listing 18.1. Full Contents of the iFriends/templates/base_site.html File

{% extends "admin/base.html" %}
{% load i18n %}

{% block title %}{{ title|escape }} | {% trans 'iFriends site admin' %}
  {% endblock %}
{% block branding %}
<h1 id="site-name">{% trans 'iFriends administration' %}</h1>
{% endblock %}
{% block nav-global %}{% endblock %}


Previous Page Next Page