Django, a high-level Python web framework, provides a powerful template engine for building dynamic HTML pages. This engine uses a simple and secure template language that allows you to create reusable and maintainable HTML templates. Let's explore an example of a Django HTML template and delve into its key features.

Django's template language is designed to be easy to learn and use, enabling developers to create complex web pages with minimal effort. It separates the presentation layer from the business logic, promoting code reusability and maintainability. Now, let's dive into the structure and components of a Django HTML template.

Django Template Structure
Django templates follow a specific structure, with the main components being placeholders, variables, and template tags. These components work together to generate dynamic HTML content.

At the core of a Django template is the HTML structure, which can include placeholders for dynamic content. These placeholders are defined using special syntax and are replaced with actual content when the template is rendered.
Placeholders and Variables

Placeholders in Django templates are denoted by curly braces, e.g., {{ placeholder }}. These placeholders are replaced with the values of Django variables. Variables can contain strings, numbers, lists, and even complex data structures like dictionaries.
For example, consider the following Django template snippet:
{{ title }}
{{ content }}
In this case, the placeholders {{ title }} and {{ content }} will be replaced with the values of the 'title' and 'content' variables, respectively, when the template is rendered.

Template Tags
Template tags are used to perform tasks like looping through lists, conditionally displaying content, or including other templates. They are enclosed in curly braces with percent signs, e.g., {% tag %}.
Here's an example of using the 'for' template tag to loop through a list of items:

{% for item in items %}
In this case, the 'for' loop iterates through the 'items' list, and for each item, it creates a new list item (li) with the item's value as its content.
Inheritance and Extending Templates




















Django's template inheritance allows you to create reusable base templates and extend them with additional content. This promotes code reuse and simplifies the process of maintaining consistent layouts across your website.
To create a base template, you define placeholders for content that will be overridden by child templates. Here's an example of a base template (base.html):
{% block title %}{% endblock %} {% block content %} {% endblock %}
In this base template, we've defined two blocks: 'title' and 'content'. Child templates can override these blocks with their own content.
Extending a Base Template
To extend a base template, you use the 'extends' template tag and specify the base template's name. Here's an example of a child template (index.html) that extends the base template:
{% extends 'base.html' %}
{% block title %}
Home - My Website
{% endblock %}
{% block content %}
Welcome to my website!
This is some example content.
{% endblock %}
In this child template, we've overridden the 'title' and 'content' blocks with our own content. When this template is rendered, it will use the HTML structure defined in the base template and replace the blocks with the content we've provided.
In conclusion, Django's HTML template engine provides a powerful and flexible way to create dynamic web pages. By understanding and leveraging its key features, such as placeholders, variables, template tags, inheritance, and extension, you can build efficient and maintainable web applications. Start exploring Django templates today and unlock the full potential of this powerful web framework!