Previous Page Next Page

Overriding the Admin Templates at the Application or Object Level

Earlier in this hour, we discussed overriding the admin templates by creating an admin folder in your own template directory. When you put customized templates, using the same name as the admin template files, in that directory, the template loader loads the custom template instead of the one in the Django installation.

That method can create problems. If you modify templates at the admin folder level, the effects are global. For example, if you create a custom admin/change_form.html file in your template directory, those same changes are seen in the change view for every model and object in your project.

More often than not, you will want to apply changes to a single model or application. Fortunately, there is a simple solution because of how Django's template system works.

You can put a custom template in the admin/application or admin/application/object folders for specific applications and objects. The custom changes are applied only to that specific application or object.

For example, if you wanted to modify the change_form.html template file for an application named Blog, you would put the custom template in the following location in your site template directory:

admin/Blog/change_form.html

The custom changes would be seen if you tried to access the add or update forms for any objects in the Blog application. However, if you wanted to use the custom forms for only the Entry object in the Blog application, you would put the template in the following location in your site template directory:

admin/Blog/Entry/change_form.html

Did you Know?

The priority in the template loader when loading template files is object, application, and then site. So you could put a different custom template at more than one level so that the entire site can use a custom template. However, a specific object may use a different custom template.


You can modify the following admin templates at the application or object level:

Previous Page Next Page