Django focuses on making it faster and easier to build a web application. One way it does this is with the Template Language that is used to format and display content.

Django comes with its own template language that is based on the popular Jinja2 template engine and adds some features that make it better suited for web development.

You can deploy a Django app online with one click

What is Django Template Language?

Most web frameworks have a template system that allows the developer to separate the presentation layer from the application logic.

With Django, this is all handled automatically, and developers are free to write their applications unhindered by concerns that have already been solved.

In the world of web development, one problem comes up over and over again: HTML.

Whenever we develop a new web application, at some point we have to display information from a database or variables from your Django app.

The most simple way to do so is to using a template language, in the case of Django the Django Template Language (DTL).

Template Language example, mix HTML with Python code

The Basic Structure of Django Template Language(DTL)

Django Template Language (DTL) is the primary way to generate output from a Django application. You can include DTL tags inside any HTML webpage.

The basic DTL tag you can include in an HTML webpage is:

1
{% Tag %}

You can use if statements, execute HTML conditonally

1
2
3
{% if %}
<code>
{% end if %}

HTML or code can be repeated using for loops

1
2
3
{% for x in y %}
<code>
{% endfor %}

You can include other files in your template:

1
{% include “header.html(file name)” %}

You can inherit from another html file:

1
{% extends “base.html(file name)” %}

To show a variable, use this syntax:

1
{{ <Variable_Name> }}

For more variable use, see below:

1
2
3
4
Simple variable : {{ title }} , {{ x }}
List attributes : {{ my_list.0 }}
Object attributes : {{ obj.title }}
Dictionary attribute : {{ dict.key }}

Comments can be included in a template too:

1
{# <Comment> #}

Why do we need the Django Template Language?

In short, templates give Django its flexibility. The DRY principle – Don’t Repeat Yourself – is important to many frameworks, but in Django templates are your friend.

When I started digging into this system, I wanted to understand why exactly I need this. What’s wrong with str.format or just using HTML?

Since I like writing programs in Python, it only makes sense that I can just write my templates as Python code.

First of all, let’s talk about security. This is by far the most important benefit of using the Template Language: potentially dangerous template code won’t be executed by the Python interpreter.

You can still use Python code in your templates but you don’t have to worry about XSS attacks and similar things.

Secondly, you can write all your settings in one place. You don’t want to mix your application case with your user-interface code, as it’s harder to maintain.

You can deploy a Django app online with one click