Django Template Language
We have used Django templates and static files to display static content to our users. We’ve done all this by using HTML. but it is not possible to display dynamic content using HTML. So, that’s where the concept of DTL (Django Template Language) comes. we can render the dynamic information on our website using variables and tags of the Django template language. This template language is based on the Jinja2 template engine.
So, during rendering all the static content of our website remains the same but, the dynamic part gets changed.
These are some elements of the Django Template Language (DTL) -
Django Variables
In Django, you can render variables by using {{ variable }} brackets. All these variables are resolved by jinja2 during the rendering of our website template. Let us understand this concept through some practical work.
import datetime
def contact(request) :
dt = datetime.datetime.now()
if request.method == "POST":
name = request.POST.get('name')
email = request.POST.get('email')
phone = request.POST.get('phone')
message = request.POST.get('message')
contact = Contact(name=name, email=email, phone=phone, message=message)
contact.save()
context = {'dt': dt,
}
return render(request, 'contact.html',context )
In the above code first, i imported DateTime. Then i had declared dt = datetime.datetime.now(). This variable will fetch the live date and time. code after this is explained in previous tutorials. Then, I created a dictionary and passed it into the render.
In my HTML file, I added this variable.
<h3> {{dt}} </h3>
Now, let's check whether our variable is working in our variable or not.
working properly.
Django Tags
Just like python you can use if, elif, else, for & while tags in Django also. You can also use block declaration tags to inherit your templates. Some examples of using Django templates are given below.
- views.py
def contact(request) :
nm = "rohit"
context = { 'nm': nm,
}
return render(request, 'contact.html',context )
Here, i have created a variable nm = “rohit”. And, then I created a dictionary “context” and passed it into the render.
- contact.html
{% if nm == "rohit" %}
<h1> welcome to our Django Tutorial </h1>
{% else %}
<h1> HELLO </h1>
{% endif %}
Here, i have used the Django tag and given a condition if nm == “rohit” then display “welcome to our Django Tutorial”. Else display “HELLO”. The output of the above code is given below.
There are many more Django tags which are given below.
- Loop Tags
These Loops are similar to python loops. And, it is used to iterate loops in variables.
{% for i in a %}
<your code>
{% endfor %}
- Block Tags
These tags are mainly used for template inheritance.
{% block Name %}
<code>
{% endblock %}
Django Filters
Django Filters are used to modify Django variables. It is displayed by using the tag {{<variable_name>||<filter_attribute>}}. Django provides almost 60 built-in template filters. Here, are some Django template filters with some practical work.
- Views.py
def contact(request) :
nm = ['a','b','c','d','e']
context = { "nm": nm,
}
return render(request, 'contact.html',context )
In above code i created a list. Then i created a dictionary also and, passed that into render. Now i will use length filter_attribute to render the length of my list.
<h1> {{ nm|length }} </h1>
- Result
These are some more filter attribute.
S.no |
Filter Attributes |
1. |
add |
2. |
addslashes |
3. |
capfirst |
4. |
center |
5. |
default |
6. |
escape |
7. |
filesizeformat |
8. |
length |
9. |
make_list |
10. |
random |
11. |
slice |
12. |
time |
13. |
unordered_list |
14. |
upper |