Day 10 of 100 Days of Django: Django Template Language Part I

Aman Khan
2 min readJul 20, 2022

Hi🙋‍♂️ Devs, Let’s start with Why we need Django Template Language(DTL)🤔.

DTL is designed to strike a balance between power and ease. It’s designed to feel comfortable to those used to working with HTML.

Jinja2 — Jinja is a modern and designer friendly templating language for python, modelled after Django's templates. It is fast widely used and secure with option sandboxed template execution environment.

By default we get DTL with Django but we can configure the settings to use Jinja2.

Syntax:

pip install jinja2# In settings.py
'BACKEND': 'django.template.backends.jinja2.Jinja2'

Variables→ Variables look like these {{variable }}When the template engine encounters a variable, it evaluates that variable and replaces it with the result.

Rules:

  • Variable names consist of any combination of alphanumeric characters and underscore.
  • Variables name should not start👨‍🎤 with underscore.
  • Variable name can not have space🚀 or punctuation characters.

Syntax:

{{variable}}
Example:{{name}},{{first_name}},{{name1}}

Filters:

When we need to modify variable before displaying📺 we can use filters. Pipe ‘|’ is used to apply filter🎞.

Syntax:

{{variable|filter}}
Example:
{{name|upper}} // It will convert the name to upper case characters.

Some filters take arguments

Syntax:

{{variable|filter:argument}}
Example:
{{article|truncateword:40}}

Filters can be chained🔗

Syntax:

{{variable|filter|filter}}
Example:
{{article|truncateword:40|upper}}

In the Next Article we are going to see List of Filters and Conditional Statements 👨‍💻. So stay tuned.

Thanks for Following and Claps😋

Link to Day 11

Link to Starter Page

--

--

Aman Khan

Blockchain and Web3 Engineer Crafting compelling stories at the intersection of tech, business, and culture.