python-backend
/

Django Templates – Building Dynamic UI

Last Sync: Today

On this page

5
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

python-backend

Django Templates – Building Dynamic UI

What is the Django Template Language (DTL)?

The Django Template Language is a text-based language designed to strike a balance between power and ease of use. It allows developers to define the structure of the UI while leaving 'holes' for data provided by the View. Because it is not pure Python, it is safe to allow designers to edit templates without risking the execution of malicious backend code.

  1. Rendering a Template

In Django, a View fetches data (the context) and passes it to a template. By default, Django looks for a 'templates' folder inside each installed app.

PythonRead-only
1
from django.shortcuts import render

def profile_view(request):
    context = {
        'name': 'Kishore',
        'projects': ['Revochamp', 'Flutter Builder'],
        'is_premium': True
    }
    return render(request, 'user/profile.html', context)

  1. Template Syntax

DTL uses four main types of constructs to handle data and logic:

  • Variables {{ variable }}: Outputs a value from the context. If the value is an object, you can use dot notation (e.g., {{ user.name }}).
  • Tags {% tag %}: Controls the logic of the template (loops, ifs, and inheritance).
  • Filters {{ var|filter }}: Modifies variables for display (e.g., {{ name|lower }}).
  • Comments {# comment #}: Internal notes that are not rendered in the final HTML.

  1. Template Inheritance

Inheritance is the most powerful part of Django's template engine. You define a 'base' skeleton and use blocks that child templates can override.

HTMLRead-only
1

<html>
  <body>
    <header>My App</header>
    {% block content %}
      
    {% endblock %}
    <footer>© 2026</footer>
  </body>
</html>


{% extends 'base.html' %}

{% block content %}
  <h1>Welcome to the Homepage</h1>
{% endblock %}

Common Template Tags & Filters

TypeSyntaxDescription
Loop{% for item in list %}Iterates over a sequence
Condition{% if user.is_authenticated %}Conditional rendering
URL{% url 'view-name' %}Reverse URL resolution (best practice)
Static{% static 'css/style.css' %}Links to static assets
Filter{{ date|date:'Y-m-d' }}Formats date objects
CSRF{% csrf_token %}Protects forms from cross-site request forgery

Test Your Knowledge

Q1
of 3

Which tag must be used at the very top of a file to inherit from a base template?

A
{% include %}
B
{% load %}
C
{% extends %}
D
{% block %}
Q2
of 3

How do you apply multiple filters to a single variable?

A
{{ name|lower|truncatechars:10 }}
B
{{ name|lower, truncatechars:10 }}
C
{{ lower(truncatechars(name, 10)) }}
D
You can only use one filter at a time
Q3
of 3

What is the purpose of the {% csrf_token %} tag?

A
To style the form
B
To provide security for POST requests
C
To identify the user's browser
D
To connect the form to the database

Frequently Asked Questions

Can I call Python methods with arguments in DTL?

No. DTL intentionally prevents this to keep logic out of the template. If you need to process data, do it in the View or create a Custom Template Filter.

What is a 'Context Processor'?

It is a function that makes specific data (like the logged-in user or site settings) available in every template automatically, without manually passing it in every view.

How do I handle missing variables?

By default, if a variable doesn't exist, Django renders it as an empty string. You can change this behavior in the settings, but it is rarely necessary.

Previous

django views

Next

django rest framework

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.