Django Template Language (DTL): Syntax & Template Inheritance

The Template represents the presentation layer (T in MVT) of a Django application. The Django Template Language (DTL) is designed to let backend engineers and frontend designers collaborate seamlessly without introducing security vulnerabilities or business logic clutter into markup.

The 4 Core DTL Syntax Elements

Syntax Pattern Code Example Description & Behavior
Variables {{ post.title }} Outputs the value of a variable or dictionary lookup passed from the view context.
Tags {% for post in posts %} ... {% endfor %} Controls rendering logic: loops, conditional branches, URL lookups, and block extensions.
Filters {{ post.content|truncatewords:30 }} Transforms the visual presentation of a variable before it is rendered to HTML.
Comments {# Single line #} or {% comment %} Internal developer notes that are never output to the final HTML delivered to browsers.

Master Template Inheritance Blueprint

Here is a production-grade base.html skeleton declaring blocks for metadata, styles, content, and scripts:

<!-- templates/base.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}Veewom Tutorials{% endblock %}</title>
    <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
    {% block extra_css %}{% endblock %}
</head>
<body>
    <nav class="navbar navbar-default">
        <!-- Global Navigation bar content -->
    </nav>

    <main class="container">
        <!-- Child templates inject their unique content here -->
        {% block content %}
        {% endblock %}
    </main>

    <footer class="footer">
        <p>&copy; 2016-2026 Veewom.com</p>
    </footer>

    <script src="{% static 'js/bootstrap.min.js' %}"></script>
    {% block extra_js %}{% endblock %}
</body>
</html>

And here is how a child template inherits the layout and overrides only the content:

<!-- templates/blog/post_detail.html -->
{% extends "base.html" %}

{% block title %}{{ post.title }} | Veewom{% endblock %}

{% block content %}
<article class="article-wrapper">
    <h1>{{ post.title }}</h1>
    <p class="text-muted">Published by {{ post.author }} on {{ post.created_at|date:"F j, Y" }}</p>
    <div class="article-body">
        {{ post.content|linebreaks }}
    </div>
</article>
{% endblock %}

Golden Rule of Template Inheritance

The {% extends 'base.html' %} tag MUST be the very first line of any child template. Putting HTML tags or spaces before {% extends %} causes Django to throw a TemplateSyntaxError.

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. What is the Django Template Language (DTL)?

DTL is Django's built-in templating engine designed to bridge Python backend data and HTML presentation. It strikes a balance between power and security by intentionally preventing arbitrary Python code execution inside HTML files.

Q2. What are the four fundamental syntax tokens in DTL?

The four tokens are: 1) `{{ variable }}` for displaying values, 2) `{% tag %}` for logic/control structures (if, for, block), 3) `{{ value|filter }}` for modifying representations, and 4) `{# comment #}` or `{% comment %}` for internal notes.

Q3. How does template inheritance work with {% extends %} and {% block %}?

A master base template (e.g. `base.html`) defines the overall skeleton (header, navbar, footer, scripts) with named placeholder slots called `{% block content %}{% endblock %}`. Child templates use `{% extends 'base.html' %}` at line 1 and override only the specific blocks they require.

Q4. How does Django protect against Cross-Site Scripting (XSS) in templates?

Django templates automatically escape HTML special characters (`<`, `>`, `&`, `'`, `"`) output via `{{ variable }}`. If a variable contains ``, Django renders it safely as `<script>...` unless explicitly marked with the `|safe` filter.

Explore Related Django Tutorials & Next Steps

Broaden your backend architecture, REST API design, and production engineering expertise:

Built-in & Custom Filters and Tags

Format dates, truncate text, and author custom template logic.

Static Files Configuration

Link CSS, JS, and image assets into base template layouts.

Context Processors in Django

Inject site-wide global variables directly into every template.

Tailwind, Bootstrap & HTMX UI

Combine Django templates with modern responsive frontend frameworks.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!