Challenge - 5 Problems
Flask Include Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Flask template using include?
Given the following Flask templates, what will be rendered when
base.html:
header.html:
main.html:
main.html is rendered?base.html:
{% raw %}{% block content %}{% endblock %}{% endraw %}header.html:
{% raw %}Welcome!
{% endraw %}main.html:
{% raw %}{% extends 'base.html' %}{% block content %}{% include 'header.html' %}Main content here.
{% endblock %}{% endraw %}Attempts:
2 left
💡 Hint
Remember that {% raw %}{% include %}{% endraw %} inserts the content of the specified template at that point.
✗ Incorrect
The {% raw %}{% include 'header.html' %}{% endraw %} tag inserts the header.html content inside the content block, so the final rendered HTML includes the header followed by the paragraph.
📝 Syntax
intermediate2:00remaining
Which option correctly includes a reusable fragment in a Flask template?
You want to include a reusable footer fragment stored in
footer.html inside your main template. Which of the following is the correct syntax?Attempts:
2 left
💡 Hint
The tag to insert another template's content is {% raw %}{% include %}{% endraw %}.
✗ Incorrect
Only {% raw %}{% include 'footer.html' %}{% endraw %} correctly inserts the content of footer.html at that point in the template.
🔧 Debug
advanced2:00remaining
Why does this Flask template raise a TemplateNotFound error?
You have this template code:
But when rendering, Flask raises
{% raw %}{% include 'nav.html' %}{% endraw %}But when rendering, Flask raises
TemplateNotFound: nav.html. What is the most likely cause?Attempts:
2 left
💡 Hint
Check if the file you want to include exists in the right place.
✗ Incorrect
Flask looks for templates in the templates folder. If nav.html is missing, it raises TemplateNotFound.
❓ state_output
advanced2:00remaining
What is the output when including a template with variables?
Given these templates:
greeting.html:
main.html:
And the Flask view renders
greeting.html:
{% raw %}Hello, {{ name }}!
{% endraw %}main.html:
{% raw %}{% include 'greeting.html' %}{% endraw %}And the Flask view renders
main.html with {'name': 'Alice'} as context, what will be the output?Attempts:
2 left
💡 Hint
Included templates share the same context as the parent template.
✗ Incorrect
The included template uses the same variables passed to the main template, so {{ name }} is replaced by 'Alice'.
🧠 Conceptual
expert3:00remaining
Why use {% raw %}{% include %}{% endraw %} instead of {% raw %}{% extends %}{% endraw %} for reusable fragments?
In Flask templates, what is the main conceptual difference between {% raw %}{% include %}{% endraw %} and {% raw %}{% extends %}{% endraw %} when building reusable parts?
Attempts:
2 left
💡 Hint
Think about whether you want to build a whole page layout or just add a small piece.
✗ Incorrect
Extends is for base templates that define structure; include is for inserting reusable pieces inside templates.