Django Templates

  • Learn how Django Templates render dynamic HTML content in web applications.
  • What are Templates in Django?

    A Template is an HTML file that:

    • Defines how data is displayed

    • Separates UI (HTML) from business logic (Python)

    Simple words:
    👉 Templates handle frontend (presentation layer) in Django

    Role of Templates in MVT Architecture

    Layer

    Role

    Model

    Database

    View

    Logic

    Template

    User Interface (HTML)

    Django sends data from View → Template

    Creating Templates in Django

    Step 1: Create Templates Folder

    Inside your app directory:

    myapp/

     ├── templates/

     │    └── myapp/

     │         └── home.html


    Best practice:

    • App name inside templates folder avoids conflicts

    Step 2: Configure Templates in settings.py

Template Configuration

Ensures Django can locate template files.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

APP_DIRS=True allows Django to search inside app templates

Step 3: Create Template File

File: home.html

<!DOCTYPE html>
<html>
<head>
    <title>Django Template</title>
</head>
<body>
    <h1>Welcome to Django Templates</h1>
</body>
</html>
  • Step 4: Render Template in View

Rendering Template

View sends HTML page as response.

from django.shortcuts import render

def home(request):
    return render(request, 'myapp/home.html')
  • Template Variables

    What are Template Variables?

    • Used to display dynamic data

    • Passed from View to Template

    • Written inside {{ }}

    Template Variable

Passing Data to Template [ views.py ]

Sending Python data to HTML.

def profile(request):
    data = {
        'name': 'Radha',
        'course': 'Django',
        'duration': '3 Months'
    }
    return render(request, 'myapp/profile.html', data)

profile.html

<h2>Name: {{ name }}</h2>
<p>Course: {{ course }}</p>
<p>Duration: {{ duration }}</p>
  • Rules of Template Variables

    • No Python code allowed

    • Only variable display

    • Safe & secure

    Template Tags

    What are Template Tags?

    Template tags:

    • Perform logic

    • Control flow

    • Written inside {% %}

    Common Template Tags

    Tag

    Purpose

    {% if %}

    Conditions

    {% for %}

    Loops

    {% block %}

    Template inheritance

    {% extends %}

    Reuse templates

    {% include %}

    Include another template

    If Tag

Conditional Rendering [ views.py ]

Displays content based on condition.

def result(request):
    return render(request, 'myapp/result.html', {'marks': 85})

result.html

{% if marks >= 40 %}
    <h3>Pass</h3>
{% else %}
    <h3>Fail</h3>
{% endif %}

Looping Data [ views.py ]

Iterates list data

def subjects(request):
    subjects = ['Python', 'Django', 'MySQL']
    return render(request, 'myapp/subjects.html', {'subjects': subjects})

subjects.html

<ul>
{% for subject in subjects %}
    <li>{{ subject }}</li>
{% endfor %}
</ul>
  • Extends & Block

Template Inheritance [ base.html ]

Avoids repetition using base template.

<html>
<body>
    <h1>My Website</h1>
    {% block content %}{% endblock %}
</body>
</html>

home.html

{% extends 'myapp/base.html' %}

{% block content %}
<h2>Home Page</h2>
{% endblock %}

Including Template

Reuses components like header/footer.

{% include 'myapp/footer.html' %}