Django HTML Template Example

Ann Jul 09, 2026

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 Templates for CRUD Views
Django Templates for CRUD Views

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.

🗑️ Template for Delete Page in Django
🗑️ Template for Delete Page in Django

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.

Django Template Language (DTL) Basics
Django Template Language (DTL) Basics

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

Django Bootstrap 4 – Integrate Template Example
Django Bootstrap 4 – Integrate Template Example

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.

14 Django Website Template | Online Training Download app from below link
14 Django Website Template | Online Training Download app from below link

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:

Basic CRUD Structure in Django
Basic CRUD Structure in Django

{% for item in items %}
  
  • {{ item }}
  • {% endfor %}

    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

    Chapter 12: Implementing Schema Markup
    Chapter 12: Implementing Schema Markup
    Django Project Structure Best Practice 2022
    Django Project Structure Best Practice 2022
    🔗 Django Models: OneToOneField (One-to-One Relationship)
    🔗 Django Models: OneToOneField (One-to-One Relationship)
    Add Links with the {% url %} Tag in Django Templates
    Add Links with the {% url %} Tag in Django Templates
    📄 Template with Model Relations in Django
    📄 Template with Model Relations in Django
    the diagram shows how to use django
    the diagram shows how to use django
    Customizing Views: Filter Lists in Django Admin
    Customizing Views: Filter Lists in Django Admin
    the bootstrap 4 dashboard is displayed in front of a cityscape background
    the bootstrap 4 dashboard is displayed in front of a cityscape background
    Django: MVT(Model-View-Template)
    Django: MVT(Model-View-Template)
    django-advanced-filters on Pypi
    django-advanced-filters on Pypi
    a diagram with the words django in different languages
    a diagram with the words django in different languages
    Learn Django Project  and File Structure
    Learn Django Project and File Structure
    ⚙️ Django Generic Views – Build Faster with Less Code
    ⚙️ Django Generic Views – Build Faster with Less Code
    Django Models – choices Option
    Django Models – choices Option
    Django tips
    Django tips
    Django vs Flask: Which Python Framework Should You Learn First?
    Django vs Flask: Which Python Framework Should You Learn First?
    Django Ecommerce with Source Code
    Django Ecommerce with Source Code
    DJANGO VS FASTAPI (2026)
    DJANGO VS FASTAPI (2026)
    Add Google Login to Your Django App with Allauth
    Add Google Login to Your Django App with Allauth
    Understanding Django's MVT Framework
    Understanding Django's MVT Framework

    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!