Django template language – django tutorial


Django is a python web development framework, it is very secure and fully loaded framework. we can develop our projects easily. In this Django tutorial, we discuss the Django template language.

A Django template is a text file that can generate any type of text-based formats like HTML, XML, and CSV. we wrote all HTML code in templates and also we send our data from our database to templates.

Django templates are useful to display to the data but these are slightly different from HTML pages because we send data from our Django models. so we must include our data into Django templates.

If you want to know how to include our data into the Django template, you must know the Django template language.

Django template language

There are different things to know before you include data into Django templates. Here I mentioned the most common things of django template language.

1. Template Inheritance

we can inherit the base templates in django templates. In a django project we need to display home navbars and footers all the time and in all pages. But we don’t need to write those lines in every HTML page. Django templates allow us to inherit the base templates to other templates.


{% extends 'example.html' %} 

# this example.html file is our parent file.
# display.html file is our child file

2. Static files

we need to apply styles and colors to our templates all the time, so we need to add CSS, js and other files. so we must include the below line to our templates.

{% load static %}

3. Blocks

Blocks are really different and special things in django template language. we can create a block for certain code in our django templates. By creating blocks we can restrict the content to other templates and at the same time, we can allow them to show.

Blocks are used to change the content dynamically in static django templates. Creating blocks are very simple.

{% block title %}

<title> home</title>

{% endblcok %}

{% block content %}

<h1> </h1>

{% endblock %}

we must close the block all the time whenever you create a new block in django templates by using {% endblock %}.

Check out my best articles on Django

4. Variables

we can send python variables to Django templates. we must define our variables in views and send them to templates.

def home(request):
    return render(request,'index.html',{'name':''})

#HTML file


if you run the code you will get the output like below.


5. for loop

we can write for loops inside the HTML pages using django template language. yes, it is possible to include our python for loops into django templates.

 {% for i in 'hello' %}
 {% endfor %}


{% endfor %}: This is the for ending, we must specify all the statements above this statement.

6. If – elif -else condition:

we can write an if-else condition in django templates. The below code we can write directly write on our templates.

{% if False  %}
{%elif False %}
{% else %}
   <h3>else condition </h3>    
{% endif %}

else condition 

{% endif %}: This is the ending of the if condition, we must mention this statement wherever we use if condition. we should write all the statements above this line in if condition.

7. Dictionary / Lists / tuples

we can pass list/dict/tuple from views to templates easily. we can iterate the data through loops.

def home(request):

    data = {'name':'Prasad','age':25}
    return render(request,'index.html',{'data':data})

{% for i in data.values %}
{% endfor %}

#we can pass tuples and lists also.

8. URLs

URLs are the most important things in our templates because URLs can help us to link other djnago templates in our django project. But Django URLs are different from basic HTML URLs. Django template language provides different types of URLs for django project.

def home(request):
    return render(request,'index.html'{})

        path('',home,name='home'), # name help us to create urls.


<a href={% url 'home' %}>Home</a>

8. counter

If you want count no.of elements in a list when you iterate a list item you just add this line in for loop.

{% for color in colors %}

<p>{{forloop.counter}} : {{color}}</p>

{% endfor %}

These are the most common things in django template language. check out more here Documentation.

If you like this article, please appreciate ur through comment and subscribe to our newsletter. Thank you..!

Leave a Reply