Blocks let you create parts of a webpage that can be changed later. This helps you reuse layouts and change only what you want.
Block definitions and overriding in Flask
{% block block_name %}
Default content here
{% endblock %}
<!-- To override in child template -->
{% extends 'base.html' %}
{% block block_name %}
New content here
{% endblock %}Use {% block %} to define a replaceable section in your base template.
Use {% extends %} in child templates to inherit and override blocks.
title with default text 'My Site'.{% block title %}My Site{% endblock %}title block in the base template to show 'Home Page'.{% extends 'base.html' %}
{% block title %}Home Page{% endblock %}{% block content %}
<p>Welcome to my website!</p>
{% endblock %}This example shows a base template with blocks for title and content. The home.html template extends the base and overrides these blocks to show a custom title and content.
{# base.html #}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}Default Title{% endblock %}</title>
</head>
<body>
<header>
<h1>My Website</h1>
</header>
<main>
{% block content %}
<p>This is the default content.</p>
{% endblock %}
</main>
<footer>
<p>Ā© 2024 My Website</p>
</footer>
</body>
</html>
{# home.html #}
{% extends 'base.html' %}
{% block title %}Home Page{% endblock %}
{% block content %}
<p>Welcome to the home page!</p>
{% endblock %}Blocks help keep your templates clean and avoid repeating code.
If a block is not overridden, the default content inside the block is shown.
Always use {% extends %} at the very top of your child templates.
Blocks define parts of a template that can be changed in child templates.
Use {% block %} and {% endblock %} to create blocks.
Use {% extends %} to inherit and override blocks in other templates.
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
