NelsonLabs
Django/Templates

Templates

Django's template language lets you build dynamic HTML โ€” looping over data, conditionally showing elements, extending base layouts โ€” all in HTML-like syntax.

base.html โ€” template inheritance
html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>{% block title %}NelsonLabs{% endblock %}</title>
  <link rel="stylesheet" href="{% static 'css/style.css' %}" />
</head>
<body>
  <nav>
    <a href="{% url 'course-list' %}">Courses</a>
  </nav>

  <main>
    {% block content %}
    {# Child templates replace this block #}
    {% endblock %}
  </main>

  <footer>
    <p>&copy; {{ year }} NelsonLabs</p>
  </footer>
</body>
</html>
courses/list.html โ€” child template
html
{% extends "base.html" %}

{% block title %}All Courses โ€” NelsonLabs{% endblock %}

{% block content %}
<h1>Courses</h1>

{% if courses %}
  <ul>
    {% for course in courses %}
      <li>
        <a href="{% url 'course-detail' slug=course.slug %}">
          {{ course.title }}
        </a>
        <span>{{ course.level }}</span>
        {% if course.is_live %}
          <span class="badge">Live</span>
        {% endif %}
      </li>
    {% endfor %}
  </ul>
{% else %}
  <p>No courses available yet.</p>
{% endif %}
{% endblock %}

NOTE

Key template syntax. {{ variable }} โ€” display a value. {% tag %} โ€” logic (if, for, block, extends, include). {# comment #} โ€” not rendered. {{ value|filter }} โ€” transform a value (e.g., {{ title|upper }}, {{ date|date:'Y-m-d' }}).