Template inheritance lets you create a base template with common layout and extend it in child templates to reuse code and keep pages consistent.
Click to reveal answer
beginner
How do you specify a base template to extend in a Django template?
Use the {% raw %}{% extends 'base.html' %}{% endraw %} tag at the top of your child template to inherit from 'base.html'.
Click to reveal answer
beginner
What is the purpose of {% raw %}{% block %}{% endblock %}{% endraw %} tags in Django templates?
Blocks define sections in the base template that child templates can override to insert their own content.
Click to reveal answer
intermediate
If a child template does not override a block, what happens?
The content inside the block in the base template is used as default and shown in the rendered page.
Click to reveal answer
beginner
Why is template inheritance useful in web development?
It saves time by avoiding repeated code, ensures consistent look across pages, and makes maintenance easier by changing the base template once.
Click to reveal answer
Which tag do you use to inherit from a base template in Django?
A{% raw %}{% include 'base.html' %}{% endraw %}
B{% raw %}{% block 'base.html' %}{% endraw %}
C{% raw %}{% extends 'base.html' %}{% endraw %}
D{% raw %}{% import 'base.html' %}{% endraw %}
✗ Incorrect
The {% raw %}{% extends %}{% endraw %} tag is used to inherit from a base template.
What does the {% raw %}{% block %}{% endblock %}{% endraw %} tag do in a base template?
ADefines a section that child templates can override
BIncludes another template
CAdds a comment
DStarts a loop
✗ Incorrect
Blocks mark sections in the base template that child templates can replace with their own content.
If a child template does not override a block, what content is shown?
AThe block content from the base template
BAn error is shown
CThe block is empty
DThe child template content is shown
✗ Incorrect
The base template's block content acts as default if the child does not override it.
Where should the {% raw %}{% extends %}{% endraw %} tag be placed in a Django template?
AAnywhere in the template
BAt the very top of the template
CInside a block
DAt the bottom of the template
✗ Incorrect
The {% raw %}{% extends %}{% endraw %} tag must be the first tag in the template.
What is a key benefit of using template inheritance?
AAutomatically styling pages
BMaking templates run faster
CAdding JavaScript functionality
DReusing common layout and reducing repeated code
✗ Incorrect
Template inheritance helps reuse layout and avoid repeating the same HTML in many files.
Explain how template inheritance works in Django using a base template and child templates.
Think about how a master page sets the frame and others fill in details.
You got /4 concepts.
Describe why using a base template with blocks is better than copying the same HTML in every page.
Imagine changing a header on 100 pages vs just one file.
You got /4 concepts.
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.