📖 Scenario: You are building a simple website with multiple pages that share the same header and footer. To avoid repeating the same HTML code on every page, you will use Django's template inheritance feature.
🎯 Goal: Create a base template called base.html with a header and footer. Then create a child template called home.html that inherits from base.html and fills in the main content area.
📋 What You'll Learn
Create a base template base.html with a header, footer, and a block named content.
Create a child template home.html that extends base.html.
Override the content block in home.html with a welcome message.
Use correct Django template syntax for inheritance and blocks.
💡 Why This Matters
🌍 Real World
Websites often share common page parts like headers and footers. Template inheritance lets developers write these parts once and reuse them, saving time and reducing errors.
💼 Career
Understanding template inheritance is essential for Django developers to build maintainable and scalable web applications.
Progress0 / 4 steps
1
Create the base template base.html
Create a file named base.html. Inside it, write HTML with a <header> containing the text My Website Header, a <footer> containing the text My Website Footer, and a Django template block named content between them. Use the exact block syntax: {% block content %}{% endblock %}.
Django
Hint
Use HTML tags <header> and <footer>. Put {% block content %}{% endblock %} between them.
2
Create the child template home.html and extend base.html
Create a file named home.html. At the top, write the Django template tag to extend base.html exactly as {% extends "base.html" %}.
Django
Hint
Use the {% extends "base.html" %} tag at the top of home.html.
3
Override the content block in home.html
In home.html, add a block named content using {% block content %} and {% endblock %}. Inside this block, write the text Welcome to the Home Page! exactly.
Django
Hint
Use the block tags to override the content block and add the welcome text inside.
4
Complete the template inheritance setup
Ensure home.html starts with {% extends "base.html" %} and contains the content block with the welcome message. The base template base.html should have the header, footer, and the content block. This completes the template inheritance setup.
Django
Hint
Check that home.html extends base.html and overrides the content block. Also check that base.html has header and footer.
Practice
(1/5)
1. What is the main purpose of using {% extends %} in a Django template?
easy
A. To define a new block inside the template
B. To include JavaScript files in the template
C. To inherit the layout and content from a base template
D. To load static files like images or CSS
Solution
Step 1: Understand the role of {% extends %}
This tag tells Django to use another template as the base layout for the current template.
Step 2: Compare with other options
Options B, C, and D describe different template tags or functions unrelated to inheritance.
Final Answer:
To inherit the layout and content from a base template -> Option C
Quick Check:
{% extends %} = inherit base template [OK]
Hint: Remember: extends means 'use base template' [OK]
Common Mistakes:
Confusing extends with include
Thinking extends loads static files
Mixing block and extends tags
2. Which of the following is the correct syntax to define a block named 'content' in a Django template?
easy
A. {% content block %} ... {% endcontent %}
B. {% block 'content' %} ... {% endblock %}
C. {% define content %} ... {% enddefine %}
D. {% block content %} ... {% endblock %}
Solution
Step 1: Recall block syntax in Django templates
The correct syntax uses {% block blockname %} and closes with {% endblock %} without quotes.
Step 2: Check each option
{% block content %} ... {% endblock %} matches the correct syntax. Options A, C, and D use invalid tags. {% block 'content' %} ... {% endblock %} incorrectly uses quotes around the block name.
A. Incorrect closing tag for block; should be {% endblock %} not {% endblock content %}
B. Missing {% load static %} tag
C. The extends tag must be at the end of the file
D. Block name 'content' is invalid
Solution
Step 1: Check block closing syntax
The correct way to close a block is with {% endblock %} without repeating the block name.
Step 2: Verify other options
Missing {% load static %} tag is unrelated to block syntax. The extends tag must be at the end of the file is false; extends must be at the top. Block name 'content' is invalid is incorrect; 'content' is a valid block name.
Final Answer:
Incorrect closing tag for block; should be {% endblock %} not {% endblock content %} -> Option A
Quick Check:
Block ends with {% endblock %} only [OK]
Hint: End blocks with just {% endblock %} [OK]
Common Mistakes:
Adding block name after endblock
Placing extends tag incorrectly
Confusing load static with block syntax
5. You have a base template with blocks header, content, and footer. You want a child template to reuse the base layout but add a sidebar inside the content block without losing the original content. Which is the correct way to do this?
hard
A. {% raw %}{% extends 'base.html' %}{% block sidebar %}Sidebar content{% endblock %}{% endraw %}
B. {% raw %}{% extends 'base.html' %}{% block content %}{{ block.super }}
Sidebar content
{% endblock %}{% endraw %}
C. {% raw %}{% extends 'base.html' %}{% block content %}
Sidebar content
{% endblock %}{% endraw %}
D. {% raw %}{% extends 'base.html' %}{% block content %}Sidebar content{% endblock %}{% endraw %}
Solution
Step 1: Understand how to keep base content in a block
Using {{ block.super }} inside a block includes the original content from the base template's block.
Sidebar content{% endblock %}{% endraw %} correctly adds sidebar content after the original content. Options B and D replace the content entirely. {% raw %}{% extends 'base.html' %}{% block sidebar %}Sidebar content{% endblock %}{% endraw %} tries to add a new block 'sidebar' which doesn't exist in base, so it won't appear.