Complete Django Template Language Tutorial (DTL)
May 13, 2021 2022-12-19 9:51Complete Django Template Language Tutorial (DTL)
Complete Django Template Language Tutorial (DTL)
Complete Django Template Language Tutorial (DTL)
For our readers and beginners students who want to learn Django from a basic level, for that we are providing a Django Template Language tutorial. It will be a single post but if we need then we will create a separate section to teach DTL in detail with examples. It is very easy to use the Django variable in the template because Django separates the Python coding and HTML coding because both have different ways of writing code.
Now, we have to learn DTL (Django template language). The benefit of learning DTL is that we can use different variables coming from the view section in the Django template. If you want to send any variable from views.py file to template file, then we use DTL methods which a language that has special syntax that you have to follow while writing Django template language coding. There are different types of empales in DTL like to run a loop, make conditions, filter something, print any variable in the template.
Render a Django Template | Django Template Language Tutorial
For example, you want to render a template in Django, you should know there are three parameters that are used in the render function, that is:
render (request, template path, context)
- The request means you are requesting a web server to get a web page, it is required.
- Template path means you have to set the path of your template, it is also required.
- Context is the dictionary, in which you pass different variables in the form of a dictionary, then this dictionary key used as a variable in the template, it is an optional parameter because it depends upon your need.
- This function is used in both class-based views and function-based views also
For example
render(request, ‘folder/index.html’, {‘name’:’Faisal’})
So, the name is the key and ‘Faisal’ is the value, so you can use the name key in the HTML template as a variable. If you want to set a variable in the Django template then you have to follow a rule of curly braces, so in a proper way, you should use the name variable in the template like this one.
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>Welcome to website</h1>
<p>{{name}}</p> <!– This will show its value as ‘Faisal’ in this case –>
</body>
</html>
Varibale in DTL (Django Template Language )
If you are familiar with a programming language that you should know what is variable in fact. Variable is the container or place n which value is a store and its value may change during program execution. We want to say that, here you need to understand the meaning of variable in terms of Django. In Django, the variable key of the dictionary is stored. And you know, every key in the dictionary have their value. So when we print a key of the dictionary, then the Python interpreter displays its value. So we also get a dictionary key passing in the render function to send a template file.
When dictionary key is reached to a template, it becomes a variable, So to display a variable, we used the special syntax of DTL as {{variabel_name}}
For example:
<h1>My name is {{name}}</h1>
{{name}} will give a result of its value, as in this case, its value is “Faisal”.
Filter in Django Template Language
We can use also a filter in the Django template, which means you have to filter out some results. {{string|lower}} is the filter which means it will convert string to lower way.
Tags in Django Template Language (DTL)
In the JafriCode Django Template Language tutorial series, we clear each and everything as beginners need. So, tags also used in the Django Template Language, for example, if you want to make a condition like if-else, or want to run a loop, etc. You should use DTL tags.
The following types of tags we used in Django tempatleÂ
- Making a condition
- Running a loop
- Including a file
- Extending a file
- Creating a URL
- Much more
In this Django tutorial, we provide only examples with explanations with source code. But when update this tutorial, we will explain other types of tags also.
{%Â if condition %}
You have to run a block of HTML CSS
{% else %}
You have to run a block of HTML CSS
{%Â endif %}
Conclusion
We provided Django template langue tutorials, Sure, you have understood what we shared with you. As we update our tutorials for our readers, we also update the DTL tutorials from time to time. If you have any questions or confusion, then submit your query through our email, or submit your thoughts in the comment section, we will appreciate you!
Thank you to take the time!
Share Django Template Language Tutorial to your friends on social media networks e.g Facebook, Twitter, LinkedIn