Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Django Template Language – Introduction for Beginners

Django Template Language (2)

In this article, we will learn about the Django Template language and how to use it in Templates.

What is Django Template Language?

Django Template Language or DTL is a text-based Template language that provides a bridge between scripts like HTML, CSS, JS, etc. and programming languages like python.

DTL is specifically built for developers to embed Django logic codes into HTML template files.

DTL also has a great advantage over other text-based template languages because of its

  • Simplicity
  • Easy to learn the syntax
  • extensibility

Why do we need the Django Template Language?

A web application has two major components:

  1. Front-end
  2. Back-end

Therefore, it makes a lot more sense if the Front-end developers work separately on the HTML part while the Back-end developers work separately on the Python-Django part.

Django Template Language enables to do you exactly that!!

With DTL, a Front-end developer does not need to know python and a back-end programmer won’t be required to know HTML.

A front-end guy can work only on the HTML and leave HTML comments wherever he requires information from Django. Later a back-end guy will replace the HTML comments with the DTL syntaxes and hence won’t need HTML knowledge.

The Basic Structure of Django Template Language(DTL)

The DTL syntax is very similar to Python. It consists of :

  • Template tags
  • Template variables
  • Template Filters
  • Template Comments

We will now look into each one of them individually.

1. Template tags

Template Tags carry-outs a function or a process. That is, they “do” something. The Template Tag syntax:

{% Tag %}

Template Tags itself is of 5 different types:

1.1 Conditional statements 

These, similar to the conditional statements in Python, are to used to execute logic.

An example is shown below:

{% if %}
    <code>
{% end if %}

1.2 Loops 

This, similar to the python loop, is used to iterate variables in a loop.

{% for x in y %}
    <code>
{% endfor %}

1.3 Block Declarations

Block declarations are mainly used in Template Inheritance.

The syntax is as shown:

{% block content %}
    <code>
{% endblock %}

1.4 File Inclusions

This command includes other HTML files into the present file.

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

1.5 File Inheritance

The below command inherits other HTML files into the present file.

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

2. Template variables

Template Variables in DTL functions similar to variables in Python. The syntax:

{{ <Variable_Name> }}

Some of the template variable examples are given below:

  • Simple variable : {{ title }} , {{ x }}
  • List attributes : {{ fruits_list.0 }}
  • Object attributes : {{ name.title }}
  • Dictionary attribute : {{ dict.key }}

The data for these variables is pulled directly from the Python code and values can be implemented in the HTML code by using the above syntax.

3. Template Filter

Template filter is used to perform filtering on the template variables. The syntax for Template filters:

{{ <Variable_Name> | <filter_by_attribute> }}

Some of the most used examples of Template Filters are:

  • Change cases : {{ name|title }}, {{ characters|upper_case }}
  • List filters/slicing : {{ list|slice = “ :5 “ }}
  • Truncation : {{ name|truncatewords : 80 }}
  • Default : {{ value|default =”0” }} 

4. Template Comments

As the name suggests, this the DTL equivalent of the python comments. The Templat Comment syntax:

{# <Comment> #}

Just like in python, the code present inside the comment attribute will not be executed by the console.

Conclusion

That’s it, guys !! This was all about the Django Template Language. With this, you can efficiently link HTML codes with Python. Do check out the official documentation about the Django Template Language.

See you in the next article!! Till then keep practicing !!