Discover how to stop repeating yourself and make website updates a breeze!
Why Block definitions and overriding in Flask? - Purpose & Use Cases
Imagine building a website where every page shares the same header and footer, but the main content changes. You try copying and pasting the full HTML for each page, changing only the middle part.
This manual copying leads to lots of repeated code. If you want to update the header, you must change every page separately. It's slow, error-prone, and hard to keep consistent.
Block definitions and overriding let you create a base template with placeholders (blocks). Each page can fill or replace these blocks, so shared parts stay in one place and only unique parts change.
<html><body><header>Site header</header><main>Page 1 content</main><footer>Site footer</footer></body></html> <html><body><header>Site header</header><main>Page 2 content</main><footer>Site footer</footer></body></html>
{% extends 'base.html' %}
{% block content %}Page 1 content{% endblock %}
<!-- base.html -->
{% block content %}{% endblock %}This makes your website easy to maintain and update, letting you focus on unique page content while keeping the shared layout consistent.
Think of a blog where every post has the same look but different text. Using blocks, you write the header and footer once, then just override the post content for each article.
Blocks let you define placeholders in templates.
Overriding blocks lets pages customize parts without repeating all code.
This keeps your site consistent and easy to update.
Practice
{% block %} in Flask templates?Solution
Step 1: Understand block usage in Flask templates
The{% block %}tag defines a section in a base template that child templates can replace or extend.Step 2: Identify the purpose of blocks
Blocks allow flexible template inheritance by letting child templates override specific parts without rewriting the whole template.Final Answer:
To define sections that child templates can override -> Option BQuick Check:
Blocks = override sections [OK]
- Confusing blocks with comments
- Thinking blocks run Python code
- Using blocks to include CSS
content in a child template?Solution
Step 1: Recall block override syntax
To override a block, use{% block block_name %}...{% endblock %}in the child template.Step 2: Check each option
Only {% block content %}New content{% endblock %} uses the correct{% block %}and{% endblock %}tags with the block name.Final Answer:
{% block content %}New content{% endblock %} -> Option AQuick Check:
Override block = {% block %}...{% endblock %} [OK]
- Using {% extends %} instead of {% block %} to override
- Using non-existent tags like {% override %}
- Confusing {% include %} with block override
base.html with:{% raw %}{% block title %}Base Title{% endblock %}{% endraw %}and child template child.html with:{% raw %}{% extends 'base.html' %}{% block title %}Child Title{% endblock %}{% endraw %}What will be displayed in the
title block when rendering child.html?Solution
Step 1: Understand template inheritance
The child template extends the base and overrides thetitleblock.Step 2: Determine which block content is used
Since the child overrides the block, the content shown is from the child: "Child Title".Final Answer:
Child Title -> Option CQuick Check:
Child block overrides base block [OK]
- Assuming base block content shows instead
- Thinking both contents concatenate automatically
- Expecting a syntax error
{% raw %}{% extends 'base.html' %}{% block content %}Hello{% 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: Identify the error in the code
The code uses{% endblock content %}, which is invalid and causes a syntax error.Final Answer:
Incorrect block closing tag; should be {% endblock %} not {% endblock content %} -> Option AQuick Check:
Block ends with {% endblock %} only [OK]
- Adding block name in endblock tag
- Omitting {% block %} tag
- Wrong quotes in extends
{% raw %}{% block header %}Base Header{% endblock %}{% block footer %}Base Footer{% endblock %}{% endraw %}You want a child template to override only the
footer block but keep the base header. Which is the correct child template code?Solution
Step 1: Understand block overriding behavior
Only blocks defined in the child template override the base. Blocks not overridden use base content.Step 2: Check which blocks are overridden
{% raw %}{% extends 'base.html' %}{% block footer %}Child Footer{% endblock %}{% endraw %} overrides onlyfooter, soheaderremains from base.Final Answer:
{% extends 'base.html' %}{% block footer %}Child Footer{% endblock %} -> Option DQuick Check:
Override only needed blocks in child [OK]
- Overriding blocks unnecessarily
- Forgetting {% extends %} in child
- Defining blocks without extends
Ready to prove it?
Pass the test and this pattern moves unknown
