The base.html file defines the common page layout with blocks for title and content. The home.html file inherits from base.html and fills in the title and content blocks with page-specific information.
Always put {% extends %} at the very top of your child template file.
Blocks in the base template act like placeholders that child templates can fill.
If a child template does not override a block, the base template's content for that block is used.
Summary
Template inheritance lets you reuse common page parts easily.
Use {% extends %} to inherit and {% block %} to define replaceable sections.
This keeps your templates clean and easy to maintain.
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.