Challenge - 5 Problems
Template Includes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What will be rendered by this Django template?
Given the following templates, what will be the final rendered HTML output when rendering
base.html?Django
{% raw %}
<!-- base.html -->
<html>
<body>
{% include 'header.html' %}
<main>
<h1>Welcome</h1>
</main>
{% include 'footer.html' %}
</body>
</html>
<!-- header.html -->
<header>
<nav>Menu</nav>
</header>
<!-- footer.html -->
<footer>
<p>Contact us</p>
</footer>
{% endraw %}Attempts:
2 left
💡 Hint
Remember that {% include %} inserts the exact content of the included template at that position.
✗ Incorrect
The {% include %} tag inserts the full content of the specified template. So header.html and footer.html content appear exactly where included.
📝 Syntax
intermediate1:30remaining
Which option correctly includes a template with a variable context?
You want to include
sidebar.html and pass a variable user_name with value "Alice". Which is the correct syntax?Attempts:
2 left
💡 Hint
Check the Django documentation for the correct keyword to pass variables in include.
✗ Incorrect
The correct syntax to pass variables to an included template is {% include 'template' with var=value %}.
🔧 Debug
advanced2:00remaining
Why does this include not render the variable?
Given
But the output shows
profile.html contains Hello, {{ user_name }}! and you include it as:{% include 'profile.html' %}But the output shows
Hello, !. Why?Attempts:
2 left
💡 Hint
Think about how variables are shared between templates when using include.
✗ Incorrect
Variables must be passed explicitly or be in the current context. If user_name is missing, it won't show in the included template.
🧠 Conceptual
advanced1:30remaining
What is the main benefit of using template includes in Django?
Why do developers use {% include %} tags in Django templates?
Attempts:
2 left
💡 Hint
Think about how you avoid repeating the same code in many places.
✗ Incorrect
Includes help reuse common parts like headers, footers, or menus, so you write them once and include everywhere.
❓ state_output
expert2:30remaining
What is the output of this nested include with variable override?
Consider these templates:
What will be the rendered HTML when rendering
{% raw %}
{% include 'section.html' with title='Main Title' %}
{{ title }}
{% include 'subsection.html' with title='Sub Title' %}
{{ title }}
{% endraw %}What will be the rendered HTML when rendering
main.html?Attempts:
2 left
💡 Hint
Variables passed with 'with' override only for that include and its children.
✗ Incorrect
The first include sets title='Main Title' for section.html. Then section.html includes subsection.html passing title='Sub Title'. So section.html shows Main Title, subsection.html shows Sub Title.