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.