Bird
Raised Fist0
Djangoframework~20 mins

Template inheritance with base template in Django - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Template Inheritance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What will this Django template render?
Given the base template and child template below, what will be the rendered output when the child template is used?
Django
{% raw %}
<!-- base.html -->
<html>
  <head><title>{% block title %}Base Title{% endblock %}</title></head>
  <body>
    <header>Header Content</header>
    <main>{% block content %}Base Content{% endblock %}</main>
    <footer>Footer Content</footer>
  </body>
</html>

<!-- child.html -->
{% extends "base.html" %}
{% block title %}Child Title{% endblock %}
{% block content %}Child Content{% endblock %}
{% endraw %}
A
&lt;html&gt;
  &lt;head&gt;&lt;title&gt;Base Title&lt;/title&gt;&lt;/head&gt;
  &lt;body&gt;
    &lt;header&gt;Header Content&lt;/header&gt;
    &lt;main&gt;Base Content&lt;/main&gt;
    &lt;footer&gt;Footer Content&lt;/footer&gt;
  &lt;/body&gt;
&lt;/html&gt;
B
&lt;html&gt;
  &lt;head&gt;&lt;title&gt;Child Title&lt;/title&gt;&lt;/head&gt;
  &lt;body&gt;
    &lt;header&gt;Child Content&lt;/header&gt;
    &lt;main&gt;Child Content&lt;/main&gt;
    &lt;footer&gt;Footer Content&lt;/footer&gt;
  &lt;/body&gt;
&lt;/html&gt;
C
&lt;html&gt;
  &lt;head&gt;&lt;title&gt;Child Title&lt;/title&gt;&lt;/head&gt;
  &lt;body&gt;
    &lt;header&gt;Header Content&lt;/header&gt;
    &lt;main&gt;Child Content&lt;/main&gt;
    &lt;footer&gt;Footer Content&lt;/footer&gt;
  &lt;/body&gt;
&lt;/html&gt;
D
&lt;html&gt;
  &lt;head&gt;&lt;title&gt;Base Title&lt;/title&gt;&lt;/head&gt;
  &lt;body&gt;
    &lt;header&gt;Child Content&lt;/header&gt;
    &lt;main&gt;Base Content&lt;/main&gt;
    &lt;footer&gt;Footer Content&lt;/footer&gt;
  &lt;/body&gt;
&lt;/html&gt;
Attempts:
2 left
💡 Hint
Remember that {% block %} tags in the child template replace the corresponding blocks in the base template.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Django template inheritance code
Which option contains a syntax error that will cause Django template rendering to fail?
Django
{% raw %}
<!-- base.html -->
<html>
  <head><title>{% block title %}Base Title{% endblock %}</title></head>
  <body>
    {% block content %}Base Content{% endblock %}
  </body>
</html>

<!-- child.html -->
{% extends "base.html" %}
{% block title %}Child Title{% endblock %}
{% block content %}Child Content
{% endblock %}
{% endraw %}
A
{% extends "base.html" %}
{% block title %}Child Title{% endblock %}
{% block content %}Child Content{% endblock %}
B
{% extends "base.html" %}
{% block title %}Child Title{% endblock %}
{% block content %}Child Content
C
{% extends "base.html" %}
{% block title %}Child Title
{% endblock %}
{% block content %}Child Content{% endblock %}
D
}% kcolbdne %{tnetnoC dlihC}% tnetnoc kcolb %{
}% kcolbdne %{eltiT dlihC}% eltit kcolb %{
}% "lmth.esab" sdnetxe %{
Attempts:
2 left
💡 Hint
Check if all block tags are properly closed.
❓ state_output
advanced
2:00remaining
What is the output of this nested block override?
Consider these templates. What will be the output inside the
tag when rendering 'grandchild.html'?
Django
{% raw %}
<!-- base.html -->
<html>
  <body>
    <main>{% block main_content %}Base Main{% endblock %}</main>
  </body>
</html>

<!-- child.html -->
{% extends "base.html" %}
{% block main_content %}Child Main{% endblock %}

<!-- grandchild.html -->
{% extends "child.html" %}
{% block main_content %}Grandchild Main{% endblock %}
{% endraw %}
A<main>Grandchild Main</main>
B<main>Base Main</main>
C<main>Child Main</main>
D<main>Child MainGrandchild Main</main>
Attempts:
2 left
💡 Hint
The most specific child template's block content replaces the parent's block content.
🔧 Debug
advanced
2:00remaining
Why does this template raise a TemplateSyntaxError?
This child template extends 'base.html' but raises a TemplateSyntaxError. What is the cause?
Django
{% raw %}
{% extends "base.html" %}
{% block title %}Child Title
{% block content %}Child Content{% endblock %}
{% endblock %}
{% endraw %}
AThe base template is missing the 'content' block definition.
BThe 'extends' tag must be the last tag in the template.
CThe 'content' block cannot be nested inside the 'title' block.
DThe 'title' block is not properly closed before starting the 'content' block.
Attempts:
2 left
💡 Hint
Check the order and closure of block tags.
🧠 Conceptual
expert
3:00remaining
How does Django resolve blocks in multiple inheritance?
Given three templates: base.html, intermediate.html (extends base), and final.html (extends intermediate). If all define a block named 'sidebar', which block content will Django render when rendering final.html?
AThe 'sidebar' block from final.html because it overrides all parent blocks.
BThe 'sidebar' block from intermediate.html because it is the immediate parent of final.html.
CThe 'sidebar' block from base.html because it is the original definition.
DA concatenation of all 'sidebar' blocks from base.html, intermediate.html, and final.html.
Attempts:
2 left
💡 Hint
Think about which template is the most specific child in the inheritance chain.

Practice

(1/5)
1. What is the main purpose of using {% extends %} in a Django template?
easy
A. To define a new block inside the template
B. To include JavaScript files in the template
C. To inherit the layout and content from a base template
D. To load static files like images or CSS

Solution

  1. Step 1: Understand the role of {% extends %}

    This tag tells Django to use another template as the base layout for the current template.
  2. Step 2: Compare with other options

    Options B, C, and D describe different template tags or functions unrelated to inheritance.
  3. Final Answer:

    To inherit the layout and content from a base template -> Option C
  4. Quick Check:

    {% extends %} = inherit base template [OK]
Hint: Remember: extends means 'use base template' [OK]
Common Mistakes:
  • Confusing extends with include
  • Thinking extends loads static files
  • Mixing block and extends tags
2. Which of the following is the correct syntax to define a block named 'content' in a Django template?
easy
A. {% content block %} ... {% endcontent %}
B. {% block 'content' %} ... {% endblock %}
C. {% define content %} ... {% enddefine %}
D. {% block content %} ... {% endblock %}

Solution

  1. Step 1: Recall block syntax in Django templates

    The correct syntax uses {% block blockname %} and closes with {% endblock %} without quotes.
  2. Step 2: Check each option

    {% block content %} ... {% endblock %} matches the correct syntax. Options A, C, and D use invalid tags. {% block 'content' %} ... {% endblock %} incorrectly uses quotes around the block name.
  3. Final Answer:

    {% block content %} ... {% endblock %} -> Option D
  4. Quick Check:

    Block syntax = {% block name %} ... {% endblock %} [OK]
Hint: Blocks never use quotes around their names [OK]
Common Mistakes:
  • Adding quotes around block names
  • Using wrong tag names like define or content block
  • Forgetting to close the block with endblock
3. Given the base template
{% raw %}{% block title %}Base Title{% endblock %}{% block content %}Base Content{% endblock %}{% endraw %}
and a child template
{% raw %}{% extends 'base.html' %}{% block title %}Child Title{% endblock %}{% endraw %}
, what will be the rendered output for the content block when rendering the child template?
medium
A. Base Content
B. Child Title
C. Child Content
D. Empty content block

Solution

  1. Step 1: Understand block overriding in inheritance

    The child template overrides only the title block, so the content block remains as defined in the base template.
  2. Step 2: Determine output for content block

    Since the child does not override content, the base template's Base Content is used.
  3. Final Answer:

    Base Content -> Option A
  4. Quick Check:

    Unchanged blocks show base content [OK]
Hint: Only overridden blocks change output [OK]
Common Mistakes:
  • Assuming all blocks are replaced
  • Confusing title block content with content block
  • Expecting empty output if block not overridden
4. Identify the error in this child template code:
{% raw %}{% extends 'base.html' %}{% block content %}Hello World{% endblock content %}{% endraw %}
medium
A. Incorrect closing tag for block; should be {% endblock %} not {% endblock content %}
B. Missing {% load static %} tag
C. The extends tag must be at the end of the file
D. Block name 'content' is invalid

Solution

  1. Step 1: Check block closing syntax

    The correct way to close a block is with {% endblock %} without repeating the block name.
  2. Step 2: Verify other options

    Missing {% load static %} tag is unrelated to block syntax. The extends tag must be at the end of the file is false; extends must be at the top. Block name 'content' is invalid is incorrect; 'content' is a valid block name.
  3. Final Answer:

    Incorrect closing tag for block; should be {% endblock %} not {% endblock content %} -> Option A
  4. Quick Check:

    Block ends with {% endblock %} only [OK]
Hint: End blocks with just {% endblock %} [OK]
Common Mistakes:
  • Adding block name after endblock
  • Placing extends tag incorrectly
  • Confusing load static with block syntax
5. You have a base template with blocks header, content, and footer. You want a child template to reuse the base layout but add a sidebar inside the content block without losing the original content. Which is the correct way to do this?
hard
A. {% raw %}{% extends 'base.html' %}{% block sidebar %}Sidebar content{% endblock %}{% endraw %}
B. {% raw %}{% extends 'base.html' %}{% block content %}{{ block.super }}
Sidebar content
{% endblock %}{% endraw %}
C. {% raw %}{% extends 'base.html' %}{% block content %}
Sidebar content
{% endblock %}{% endraw %}
D. {% raw %}{% extends 'base.html' %}{% block content %}Sidebar content{% endblock %}{% endraw %}

Solution

  1. Step 1: Understand how to keep base content in a block

    Using {{ block.super }} inside a block includes the original content from the base template's block.
  2. Step 2: Analyze options

    {% raw %}{% extends 'base.html' %}{% block content %}{{ block.super }}
    Sidebar content{% endblock %}{% endraw %} correctly adds sidebar content after the original content. Options B and D replace the content entirely. {% raw %}{% extends 'base.html' %}{% block sidebar %}Sidebar content{% endblock %}{% endraw %} tries to add a new block 'sidebar' which doesn't exist in base, so it won't appear.
  3. Final Answer:

    {% extends 'base.html' %}{% block content %}{{ block.super }}
    Sidebar content
    {% endblock %}
    -> Option B
  4. Quick Check:

    Use {{ block.super }} to keep base block content [OK]
Hint: Use {{ block.super }} to keep base block content [OK]
Common Mistakes:
  • Overwriting block content without block.super
  • Adding blocks not defined in base template
  • Forgetting to extend the base template
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard