ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

Django Tutorial• 7 min read

Django Template Language (DTL) & Inheritance

Building clean UI hierarchies with template blocks, filters, and tags

The Django Template Language provides a clean separation of presentation and business logic. Its standout feature is template inheritance, enabling you to build master site layouts extended by child pages.

Template Syntax and Inheritance

Variables: {{ user.username }} renders the value of a context variable.

Tags: {% if ... %}, {% for item in list %}, {% csrf_token %} perform logic.

Filters: Modify variable display values, e.g., {{ course.title|title }}, {{ course.price|floatformat:2 }}.

{% extends "base.html" %}: Inherits a parent skeleton layout.

{% block content %} ... {% endblock %}: Injects child page markup into the parent placeholder block.

Master Layout and Child Page (base.html & detail.html)
<!-- templates/base.html -->
<!DOCTYPE html>
<html lang="en">
<head>
  <title>{% block title %}Codexia Academy{% endblock %}</title>
  <link rel="stylesheet" href="{% static 'css/style.css' %}">
</head>
<body>
  <nav><a href="/">Home</a> | <a href="/courses/">Courses</a></nav>
  
  <main>
    {% block content %}{% endblock %}
  </main>
</body>
</html>

<!-- templates/courses/detail.html -->
{% extends "base.html" %}

{% block title %}{{ course.title }} - Codexia{% endblock %}

{% block content %}
  <h1>{{ course.title }}</h1>
  <p>{{ course.description|linebreaks }}</p>
  <strong>Price: ${{ course.price|floatformat:2 }}</strong>
{% endblock %}
Template inheritance minimizes duplication by sharing header, footer, and navigation scaffolding.
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Which tag is placed at the top of a child template to inherit a master layout template in Django?