Django Tutorial · Display Data

Add Master Template

Learn all about Add Master Template in this comprehensive tutorial.

5 min read intermediate
  • In the previous pages we created two templates, one for listing all members, and one for details about a member.
  • Now the two templates (all_members.

The extends Tag

In the previous pages we created two templates, one for listing all members, and one for details about a member.

The templates have a set of HTML code that are the same for both templates.

Django provides a way of making a "parent template" that you can include in all pages to do the stuff that is the same in all pages.

Start by creating a template called master.html, with all the necessary HTML elements:

Do you see Django block Tag inside the <title> element, and the <body> element?

They are placeholders, telling Django to replace this block with content from other sources.

Modify Templates

Now the two templates (all_members.html and details.html) can use this master.html template.

This is done by including the master template with the {% extends %} tag, and inserting a title block and a content block:

If you have followed all the steps on your own computer, you can see the result in your own browser: 127.0.0.1:8000/members/.

If the server is down, you have to start it again with the runserver command:

python

Module quiz

2 questions
1

Which of the following is true about Add Master Template?

2

What is the most common pitfall when working with Add Master Template?

Answer all questions to submit.