Previous Page Next Page

Extending Templates

As you build your website, you will need to create several different template files for different applications and views. This can result in a lot of redundant HTML code for headers, banners, and other content that will appear on several pages. Django provides the extends tag in its template engine to solve this problem.

The extends tag allows you to create a parent template file that will contain website elements that appear in more than one HTML view. Then, as you create templates for various views, you will only need to create content for that specific view in the view's template. When the Django template parser encounters an extends tag, it parses the parent template first and then applies the child template.

The following text shows the syntax for enabling a view template to inherit from another template:

{% extends "site_base.html" %}

By the Way

The parent template, specified in quotation marks, is specified as a path relative to the paths defined in the TEMPLATE_DIRS setting in the settings.py file. If you defined c:\website\templates in the TEMPLATE_DIRS setting and you create a parent template called c:\website\templates\site\header.html, you would use the following extends statement:

{% extends "site/header.html" %}


Did you Know?

You can specify a variable name instead of a filename in the extends tag. This allows you to dynamically control what the content will be by specifying different template filenames from within the view code.


You use block tags to control where items from the child template will be placed in the parent template. The block tags need to appear in both the parent and child templates. In the parent template, the block tags designate where data, with a block with the same name, from the child template will be placed. In the child template, the block tag is used to partition a section of data and assign it a name that can be referenced from the parent.

Blocks begin with a block tag and end with an endblock tag. The following code snippet shows the syntax for creating a block and assigning it the name content:

{% block content %}
<block code here>
{% endblock %}

The best way to show you how to extend a template is through an example. Listing 7.6 defines a basic parent template.

Listing 7.6. A Basic Parent Template File, base.html

<html>
<head>
<title>{% block content %}Title{% endblock %}</title>
</head>
<body>
{% block content %}
Base Site
{% endblock %}
</body>
</html>

Listing 7.7 shows how to apply the parent template shown in Listing 7.6 in a child template.

Listing 7.7. A Basic Child Template File, view.html

{% extends "base.html" %}
{% block title %}My Template View{% endblock %}
{%block content %}
<h1> My Web Page</h1><hr>
<li>Testing templates.</li>
{% endblock %}

When the view.html template shown in Listing 7.7 is parsed, the Django template parser sees the extends tag and parses the base.html parent template. Then, as it encounters the title and content blocks in the view.html file, it applies the HTML code in those sections to the HTTP response. Listing 7.8 shows the resulting HTML code that is sent as the HTTP response.

Listing 7.8. Resulting HTML Response When view.html Extends base.html

<html>
<head>
<title> My Template View </title>
</head>
<body>
<h1> My Web Page</h1><hr>
<li>Testing templates.</li>
</body>
</html>

By the Way

If a no block in the child matches a block in the parent, the data contained in the block and endblock statements is added to the HTML response. For example, if we had not included the title block in Listing 7.7, the Django parser would have included the value contained in the title block of Listing 7.6, and the <title> HTML tag in Listing 7.8 would have been <title>Title</title>.


Try It Yourself: Add a Base Template to Your Website

In this section, you will create a base template to use for the iFriends website and modify the template you created in the preceding section to extend the new parent template.

Follow these steps to create a base template called iFriends_base.html and to change the person_details.html template into a child template that extends iFriends.html:

1.
In an editor, create and open a file called iFriends/templates/iFriends_base.html.

2.
Add the contents of Listing 7.9 to the iFriends_base.html file.

Listing 7.9. Full Contents of iFriends/templates/iFriends_base.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-us" xml:lang="en-us" >
<head>
    <link rel="stylesheet" href="style.css" />
    <title>{% block title %}iFriends{% endblock %}</title>
</head>
<body>
<table width=100% bgcolor="111177">
<tr><td>
<font size="12" color="white">iFriends Webspace</font>
</td></tr>
<tr><td>
<font size="3" color="white">Home</font>
</td></tr>
</table>
{% block content %}{% endblock %}
</body>
</html>

3.
Save the file.

4.
Open the iFriends/templates/Person/person_details.html file in an editor.

5.
Remove the following lines of code:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-us" xml:lang="en-us" >
<head>
<title>{{ p.name }}</title>
</head>
<body>
. . .
</body>
</html>

6.
Add the following lines of code, as shown in Listing 7.10, to extend the iFriends_base.html template and assign the title and content blocks:

{% extends "iFriends_base.html" %}
{% block title %}test{% endblock %}
{% block content %}
{% endblock %}

7.
Save the file.

8.
Access the following URL in a web browser to verify that the new view is working correctly, as shown in Figure 7.7:

http://127.0.0.1:8000/People/Info/2/

Figure 7.7. Web page generated by people_details.html by extending iFriends_base.html.


Listing 7.10. Full Contents of iFriends/templates/person_details.html

{% extends "iFriends_base.html" %}

{% block title %}test{% endblock %}
{% block content %}
<tr bgcolor="aabbcc"><td colspan="3">
<font size="5" color="white">Personal Information</font>
</td></tr>
<tr valign="top"><td width=30% bgcolor="aaaaaa"><font color="white" size="5">
    <li>Name: {{p.name}}</li>
    <li>Birthday: {{ p.birthday }}</li>
    <li>Gender: {{ p.gender }}</li>
    <li>Desc: {{ p.desc }} </li>
</font></td>
<td width=40% bgcolor="aa99aa"><font color="white" size="4">
    <p align="center">Comments</p>
    <p align="center">None.
</font></td>
<td width=30%>
    <h3>Contact Info</h3>
    <li>Email: {{ p.email }}</li>
    <li>Website: {{ p.favoriteURL }}</li>
</td></tr>
{% endblock %}


					  


Previous Page Next Page