</>
Skip to content
Django lessons (11/43)

Django — Templates

Template Language

Django uses its own template language with variables and tags:

<!-- templates/base.html -->
<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}My Site{% endblock %}</title>
</head>
<body>
    {% block content %}{% endblock %}
</body>
</html>

Template Inheritance

<!-- templates/blog/list.html -->
{% extends 'base.html' %}

{% block title %}Blog Posts{% endblock %}

{% block content %}
<h1>Blog Posts</h1>
{% for post in posts %}
    <h2>{{ post.title }}</h2>
    <p>{{ post.content }}</p>
{% endfor %}
{% endblock %}

Variables

{{ variable }}
{{ object.attribute }}
{{ object.method }}
{{ dict.key }}

Template Tags

TagDescription
{% if %}Conditional
{% for %}Loop
{% block %}Block definition
{% extends %}Template inheritance
{% include %}Include template
{% url %}URL resolution
{% csrf_token %}CSRF protection
{% load %}Load tags library

Filters

{{ name|upper }}
{{ text|lower|truncatewords:10 }}
{{ date|date:"Y-m-d" }}
{{ list|length }}
{{ price|floatformat:2 }}
{{ html|safe }}

Context in Views

def post_list(request):
    context = {
        'posts': Post.objects.all(),
        'title': 'Blog Posts',
    }
    return render(request, 'blog/list.html', context)

Mini Practice

  1. Create a base template with blocks
  2. Extend the base template
  3. Use variables and filters
  4. Loop through a list of items

Up Next

Continue with Template Syntax — advanced template features.

Related Topics

Frequently Asked Questions about Templates

What is Templates in Django?

Templates is a fundamental concept in Django. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Templates?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Templates.

Why is Templates important in Django?

Templates is essential for Django development. Understanding this concept will help you write better code and solve real-world problems more effectively.