Discover how one simple base template can save you hours of repetitive work!
Why Template inheritance with base template in Django? - Purpose & Use Cases
Imagine you have 10 web pages, and each one needs the same header, footer, and navigation menu. You have to copy and paste the same HTML code into every page.
Manually copying shared parts is slow and risky. If you want to change the header, you must update every page separately. This wastes time and can cause mistakes or inconsistencies.
Template inheritance lets you create one base template with common parts. Other pages can reuse it and only add their unique content. Change the base once, and all pages update automatically.
<header>...</header>
<nav>...</nav>
<!-- repeated in every page -->{% extends 'base.html' %}
{% block content %}Page specific content{% endblock %}This makes building and maintaining websites faster, cleaner, and less error-prone by reusing common layouts effortlessly.
A blog site where every post shares the same header and footer but shows different articles in the main area.
Copying shared HTML manually is slow and error-prone.
Base templates hold common layout parts once.
Child templates inherit and add unique content easily.
Practice
{% extends %} in a Django template?Solution
Step 1: Understand the role of
This tag tells Django to use another template as the base layout for the current template.{% extends %}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 CQuick Check:
{% extends %}= inherit base template [OK]
- Confusing extends with include
- Thinking extends loads static files
- Mixing block and extends tags
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.Final Answer:
{% block content %} ... {% endblock %} -> Option DQuick Check:
Block syntax = {% block name %} ... {% endblock %} [OK]
- Adding quotes around block names
- Using wrong tag names like define or content block
- Forgetting to close the block with endblock
{% raw %}{% block title %}Base Title{% endblock %}{% block content %}Base Content{% endblock %}{% endraw %} and a child template {% raw %}{% extends 'base.html' %}{% block title %}Child Title{% endblock %}{% endraw %}, what will be the rendered output for the content block when rendering the child template?Solution
Step 1: Understand block overriding in inheritance
The child template overrides only thetitleblock, so thecontentblock remains as defined in the base template.Step 2: Determine output for content block
Since the child does not overridecontent, the base template'sBase Contentis used.Final Answer:
Base Content -> Option AQuick Check:
Unchanged blocks show base content [OK]
- Assuming all blocks are replaced
- Confusing title block content with content block
- Expecting empty output if block not overridden
{% raw %}{% extends 'base.html' %}{% block content %}Hello World{% endblock content %}{% endraw %}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 AQuick Check:
Block ends with {% endblock %} only [OK]
- Adding block name after endblock
- Placing extends tag incorrectly
- Confusing load static with block syntax
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?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.Step 2: Analyze options
{% raw %}{% extends 'base.html' %}{% block content %}{{ block.super }}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.Final Answer:
{% extends 'base.html' %}{% block content %}{{ block.super }}Sidebar content{% endblock %} -> Option BQuick Check:
Use {{ block.super }} to keep base block content [OK]
- Overwriting block content without block.super
- Adding blocks not defined in base template
- Forgetting to extend the base template
Ready to prove it?
Pass the test and this pattern moves unknown
