![]() |
VOOZH | about |
A Django template is an HTML file that can also include CSS and JavaScript. It is used to create dynamic web pages. It renders data sent from views inside the HTML structure.
Django provides two ways to organize templates based on your project structure:
A single template directory at the project level can be used for simplicity, storing all templates in one central location. Configuration in settings.py
Explanation:
To use Jinja2 as the template engine instead, the 'BACKEND' can be changed to 'django.template.backends.jinja2.Jinja2'
Templates are not limited to displaying static HTML, they can also present dynamic data passed from views via a context dictionary.
Example: Consider a project called 'geeksforgeeks' with an app named 'geeks':
Define three views in geeks/views.py:
Define the URL patterns in your app’s urls.py file to link each view to a specific route:
Displays static data passed from the simple_view.
In app_name/templates/geeks.html:
Accepts user input and displays different messages based on the entered age.
In app_name/templates/check_age.html:
Uses a for loop and if condition to display even numbers from a list.
In app_name/templates/loop.html:
Output
Django Template Language (DTL) is the syntax used by Django’s built-in template engine. It allows insertion of dynamic content, implementation of logic and definition of structure directly within HTML without embedding Python code. It primarily consists of variables, tags, filters, and comments.
Variables are used to display dynamic data passed from views through the context dictionary.
Syntax:
{{ variable_name }}
Example: Variables are surrounded by {{ and }} like this:
My first name is {{ first_name }}. My last name is {{ last_name }}.
With a context of {'first_name': 'Rahul', 'last_name': 'Kumar'}, this template renders to:
My first name is Rahul. My last name is Kumar.
Tags add control flow and logic to templates, such as loops, conditionals, and inheritance.
Syntax:
{% tag_name %}
Example: Tags are surrounded by {% and %} like this:
{% csrf_token %}
Common tags include: if, for, block, extends, include, csrf_token and url.
Filters are used to modify variable output before rendering on the page.
Syntax:
{{ variable_name | filter_name }}
Example:
{{ value | length }}
If value is ['a', 'b', 'c', 'd'], the output will be 4.
Common filters: length, lower, title, date, default, truncatechars.
Comments are used to hide or disable parts of a template that should not be rendered.
Syntax:
{% comment 'comment_name' %}
{% endcomment %}
Example :
{% comment "Optional note" %}
Commented out text with {{ create_date|date:"c" }}
{% endcomment %}
Facilitates reusability and consistency by defining a common base layout that child templates can extend. Implemented using {% extends %} and {% block %} tags.
Syntax:
{% extends 'template_name.html' %}
Example: Assume the following directory structure:
dir1/
template.html
base2.html
my/
base3.html
base1.html
In template.html, the following paths would be valid: