0
0
Djangoframework~20 mins

Template includes for reusability in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Template Includes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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 %}
A
&lt;html&gt;  &lt;body&gt;    &lt;header&gt;
  &lt;nav&gt;Menu&lt;/nav&gt;
&lt;/header&gt;    &lt;main&gt;      &lt;h1&gt;Welcome&lt;/h1&gt;    &lt;/main&gt;    &lt;footer&gt;
  &lt;p&gt;Contact us&lt;/p&gt;
&lt;/footer&gt;  &lt;/body&gt;&lt;/html&gt;
B<html> <body> <main> <h1>Welcome</h1> </main> </body></html>
C<html> <body> <header>Menu</header> <main> <h1>Welcome</h1> </main> <footer>Contact us</footer> </body></html>
D<html> <body> <nav>Menu</nav> <main> <h1>Welcome</h1> </main> <p>Contact us</p> </body></html>
Attempts:
2 left
💡 Hint
Remember that {% include %} inserts the exact content of the included template at that position.
📝 Syntax
intermediate
1: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?
A{% include 'sidebar.html' user_name='Alice' %}
B{% include 'sidebar.html' with user_name='Alice' %}
C{% include 'sidebar.html' context={'user_name': 'Alice'} %}
D{% include 'sidebar.html' passing user_name='Alice' %}
Attempts:
2 left
💡 Hint
Check the Django documentation for the correct keyword to pass variables in include.
🔧 Debug
advanced
2:00remaining
Why does this include not render the variable?
Given profile.html contains Hello, {{ user_name }}! and you include it as:
{% include 'profile.html' %}
But the output shows Hello, !. Why?
AThe variable user_name was not passed to the included template, so it is empty.
BThe include tag does not support variables inside included templates.
CYou must use {% extends %} instead of {% include %} to pass variables.
DThe variable user_name must be defined inside profile.html, not passed from outside.
Attempts:
2 left
💡 Hint
Think about how variables are shared between templates when using include.
🧠 Conceptual
advanced
1:30remaining
What is the main benefit of using template includes in Django?
Why do developers use {% include %} tags in Django templates?
ATo load external CSS and JavaScript files automatically.
BTo execute Python code inside templates for dynamic logic.
CTo replace the need for views by handling all logic in templates.
DTo reuse common HTML parts across multiple pages, reducing duplication and easing maintenance.
Attempts:
2 left
💡 Hint
Think about how you avoid repeating the same code in many places.
state_output
expert
2:30remaining
What is the output of this nested include with variable override?
Consider these templates:

{% 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?
A
&lt;h2&gt;Main Title&lt;/h2&gt;
&lt;p&gt;Main Title&lt;/p&gt;
B
&lt;h2&gt;Sub Title&lt;/h2&gt;
&lt;p&gt;Sub Title&lt;/p&gt;
C
&lt;h2&gt;Main Title&lt;/h2&gt;
&lt;p&gt;Sub Title&lt;/p&gt;
D
&lt;h2&gt;&lt;/h2&gt;
&lt;p&gt;Sub Title&lt;/p&gt;
Attempts:
2 left
💡 Hint
Variables passed with 'with' override only for that include and its children.