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.
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:
admin/base.html contains most of the base HTML code for the rest of the admin templates.
admin/base_site.html extends the base.html file and includes the window and banner titles for the site.
admin/login.html displays the admin login page.
admin/index.html displays the index page of models in the admin interface.
admin/change_list.html displays the change list for a model.
admin/change_form.html displays the add and update forms for an object in a model.
admin/object_history.html displays a history of operations performed on the object.
admin/delete_confirmation.html displays a confirmation page when deleting an application.
Try It Yourself: Override the base_site.html TemplateIn 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:
Listing 18.1. Full Contents of the iFriends/templates/base_site.html File
|