Django Templates – Render, Passing Values, Conditional and Loops
The Django Templates are the convenient ways to add dynamic data inside HTML. The template goes through an interpreter which executes the template’s tags and other filters and gives HTML as a response.
Django out of the box supports a template language that can make working with HTML and Dynamic data very much easier.
Creating a Template
There isn’t any extra step to create a template as it’s configured by default. The templates are actually HTML files and the Django Template engine looks for HTML files inside /templates
a folder. You can also change the directory for templates lookup by specifying them in settings.py under TEMPLATES
list.
Passing Values from Django Views to Templates
There are some functions that you can use to pass dynamic values to the templates such as render
, render_to_string
etc. However, you have to send HttpResponse back to the browser so render
the function is always used.
The render() function can be imported using from django.shortcuts import render
.
Syntax
render(request, 'template_path', context)
Example
In views.py
from django.shortcuts import render def create_product( request ): info = { "product_name" : "Soya Oil", "product_price" : 120, } return render( request, 'templates/create.html', info )
Note
Its mandatary to pass request
object as the first parameter to render
function.
Displaying Dynamic values and adding comments in Template
The next step, would be to display the data passed from views.py and inside the create.html
file.
In app/templates/create.html
<h1>{{ product_name }}</h1> <p>Price : {{ product_price }}</p>
In views.py you have passed the values wrapped inside the dictionary info
and for access you must use the key names of the dictionary.
Commenting in Template
There is a special syntax {# this is comment #} and any value inside it will be ignored by Django Template Engine. It is recommended to use this as a comment for template-related code instead of an HTML comment.
The Template will execute even if you add an HTML comment by enclosing Django Template.
Conditional Tags for Templates
You can use the IF conditional tag and here is an example of how you will be using it.
{% if product_price > 100 %} <p>The Product is costly.</p> {% else %} <p>The Product is cheap.</p> {% endif %}
For conditional statements always use {% statement %} and always end with {% endif %}
which specifies the ending of the condition logic.
Looping Tags for Templates
You can use python for-in loop within the template tag and here is an example of how you can loop through them.
In app/`views.py
from django.shortcuts import render def list_products( request ): info = { "products" : [ { "product_name" : "Soya Oil", "product_price" : 120 }, { "product_name" : "Maggi", "product_price" : 10 }, { "product_name" : "Oranges", "product_price" : 100 }, ] } return render( request, 'templates/create.html', info )
In /templates/list.html
{% for product in products %} <p>Product Name : {{ product.name }}</p> <p>Product Price : {{ product.price }}</p> {% endfor %}