So far we have discussed customizing entire template files by replacing them with either modified copies or completely new ones. You can also customize at a block level inside the template file.
You can extend the original template at the beginning of a custom template using the extend tag. Then you only need to add the custom block tags in the custom template that need to be customized.
For example, if you only wanted to override the extrahead block tag in the change_form.html template file, you could use the following contents in the customized template:
{% extends "admin/change_form.html" %} {% block extrahead %} <your custom code> {% endblock %}
You can override any block tag in the original template with your own custom code this way. It is not always convenient to override an existing block tag.
Another way you can add different code to different applications or objects using this method is to override the full template at the site level with a customized template that includes your own custom block tags. Then you can include custom templates at the application or object level that override your custom block tags with code specific to that application.
Try It Yourself: Override Admin Templates at the Application and Object LevelIn this section, you will override the change_form.html template for the Person object in the People application to include a link to the person_form() view. To add the link, you will override the form_top block in the original template. You will also override the change_list.html template at the People application level to include a link to the blog_usage() view. You need to override the change_list.html template at the site level first to add a new custom block that can be overridden at the People application level. Use the following steps to override the change_list.html and change_form.html templates:
By the Way The Run Blog Usage Report link is displayed in both the Person and Blog change lists because the custom template resides at the People application level. The Edit Person link does not show up on the Blog change form views because the custom template resides at the Person object level. Listing 18.4. Full Contents of the iFriends/templates/admin/People/person/change_form.html File
Listing 18.5. Full Contents of the iFriends/templates/admin/People/change_list.html File
|