Template tags help you control what shows on a webpage by adding logic inside HTML. They let you repeat parts, check conditions, and organize templates.
Template tags (if, for, block, extends) in Django
{% if condition %}
...
{% elif other_condition %}
...
{% else %}
...
{% endif %}
{% for item in list %}
...
{% empty %}
...
{% endfor %}
{% block block_name %}
...
{% endblock %}
{% extends "base.html" %}Template tags are inside {% and %} and control logic or structure.
Use {% endtag %} to close tags like if, for, and block.
{% if user.is_authenticated %}
<p>Welcome back!</p>
{% else %}
<p>Please log in.</p>
{% endif %}{% for product in products %}
<li>{{ product.name }}</li>
{% empty %}
<li>No products found.</li>
{% endfor %}{% block content %}
<h1>Page Title</h1>
<p>Page content here.</p>
{% endblock %}{% extends "base.html" %}This template extends a base layout. It shows a list of products or a message if none exist. It also shows a message depending on whether the user is staff or not.
{% extends "base.html" %}
{% block content %}
<h2>Products</h2>
<ul>
{% for product in products %}
<li>{{ product.name }} - ${{ product.price }}</li>
{% empty %}
<li>No products available.</li>
{% endfor %}
</ul>
{% if user.is_staff %}
<p>You have admin access.</p>
{% else %}
<p>Welcome, customer!</p>
{% endif %}
{% endblock %}Always close your tags with {% endif %}, {% endfor %}, or {% endblock %} to avoid errors.
Use {{ variable }} to show data inside templates.
Template inheritance with {% extends %} helps keep your site consistent and easier to maintain.
Template tags let you add logic like conditions and loops inside HTML.
Use {% if %} to show content based on conditions, {% for %} to repeat content, {% block %} to define replaceable sections, and {% extends %} to reuse layouts.
They make your web pages dynamic and organized.
Practice
{% extends 'base.html' %} do in a template?Solution
Step 1: Understand the purpose of
This tag is used to inherit a base template, allowing reuse of common layout parts.{% extends %}Step 2: Differentiate from similar tags
Unlike{% include %},{% extends %}sets a parent template to extend, not just insert content.Final Answer:
It tells the template to use 'base.html' as a parent layout to build upon. -> Option BQuick Check:
Template inheritance = extends [OK]
- Confusing extends with include
- Thinking it creates or deletes files
- Assuming it copies content without structure
items?Solution
Step 1: Recall Django for loop syntax
Django uses{% for variable in list %}to loop over items.Step 2: Compare options
Only {% for item in items %} matches the correct Django template syntax for loops.Final Answer:
{% for item in items %} -> Option AQuick Check:
For loop syntax = for item in list [OK]
- Using 'foreach' or 'loop' which are not Django tags
- Wrong keyword order or missing 'in'
- Confusing with Python code syntax
{% for num in numbers %}{% if num > 2 %}{{ num }} {% endif %}{% endfor %}and the context
{'numbers': [1, 3, 2, 4]}, what will be the rendered output?Solution
Step 1: Loop through each number in the list
The loop goes over 1, 3, 2, 4 in order.Step 2: Check the condition
Only 3 and 4 are greater than 2, so only these print.num > 2for eachFinal Answer:
3 4 -> Option AQuick Check:
Filter numbers > 2 = 3 4 [OK]
- Printing all numbers ignoring the if condition
- Including numbers equal to 2
- Confusing loop variable with list
{% for item in items %}{{ item }}{% endfor %}{% endif %}Solution
Step 1: Check tag pairs
The snippet closes a for loop with {% endfor %} but then has an unmatched {% endif %} without a starting {% if %}.Step 2: Validate other tags
Variable output with {{ item }} is correct, and {% endfor %} is the right closing tag for for loops.Final Answer:
Missing {% if %} tag before {% endif %} -> Option CQuick Check:
Every {% endif %} needs a matching {% if %} [OK]
- Leaving unmatched {% endif %} tags
- Confusing {% endfor %} with {% endloop %}
- Misusing {{ }} for tags
base.html with a block named content defined as:{% block content %}Default content{% endblock %}In your child template, you want to replace this block but also add a paragraph before the default content. Which is the correct way to do this?
Solution
Step 1: Understand block overriding with super
To add content before the parent's block content, use{{ block.super }}inside the block.Step 2: Check each option
{% block content %}Intro paragraph
{{ block.super }}{% endblock %} correctly uses{{ block.super }}to include the parent's content after the paragraph.Final Answer:
{% block content %}<p>Intro paragraph</p>{{ block.super }}{% endblock %} -> Option DQuick Check:
Use {{ block.super }} to include parent block content [OK]
- Using {% super %} which is invalid
- Repeating default content manually instead of block.super
- Placing {% extends %} inside a block
Ready to prove it?
Pass the test and this pattern moves unknown
