Django Templates & Context: Build Dynamic Pages Like a Pro

Shriful-Islam
Shriful Islam
Published on Jan, 30 2026 2 min read 0 comments
image

Introduction

In Django, templates are what make your web pages dynamic and interactive.

While models define data and views define logic, templates define how your data is presented.

In this article, we’ll explore:

  • How Django templates work
  • Passing context from views
  • Template inheritance
  • Practical tips for building dynamic pages

By the end, you’ll be able to build professional-looking pages that respond to your data seamlessly.

What Is a Django Template?

A Django template is an HTML file that can include:

  • Dynamic variables
  • Logic (loops, conditionals)
  • Filters and tags

Templates allow separation of presentation from business logic, following the MVC/MVT pattern.

Passing Context from Views

Views send data to templates via context dictionaries.

Example:

from django.shortcuts import render
from .models import BlogPost

def home(request):
    posts = BlogPost.objects.all()
    context = {'posts': posts, 'title': 'My Blog'}
    return render(request, 'blog/home.html', context)

In the template:

<h1>{{ title }}</h1>
<ul>
  {% for post in posts %}
    <li>{{ post.title }} by {{ post.author.username }}</li>
  {% empty %}
    <li>No posts available.</li>
  {% endfor %}
</ul>

Template Inheritance

Django supports template inheritance, which avoids duplication.

Base Template (base.html):

<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}My Site{% endblock %}</title>
</head>
<body>
    <header>
        <h1>My Site</h1>
    </header>

    <main>
        {% block content %}{% endblock %}
    </main>

    <footer>
        &copy; 2025 My Site
    </footer>
</body>
</html>

Child Template (home.html):

{% extends 'base.html' %}

{% block title %}Home - My Blog{% endblock %}

{% block content %}
<h2>Latest Posts</h2>
<ul>
  {% for post in posts %}
    <li>{{ post.title }}</li>
  {% endfor %}
</ul>
{% endblock %}

Benefits of Template Inheritance:

  • Reusable layout
  • Easy maintenance
  • Cleaner code

Using Template Tags and Filters

Built-in Template Tags

  • {% for %} – Loop through lists
  • {% if %} – Conditional rendering
  • {% include %} – Insert another template
  • {% url %} – Generate URLs dynamically

Example:

<a href="{% url 'blog:post_detail' post.id %}">{{ post.title }}</a>

Built-in Filters

  • {{ value|lower }} → lowercase
  • {{ value|truncatechars:50 }} → truncate text
  • {{ value|date:"F j, Y" }} → format date

Filters make templates powerful and flexible.

Dynamic Pages with Context

Example: Personalized greeting

def dashboard(request):
    user = request.user
    context = {'username': user.username, 'notifications': 5}
    return render(request, 'dashboard.html', context)

Template:

<h1>Welcome, {{ username }}!</h1>
<p>You have {{ notifications }} new notifications.</p>

Best Practices for Templates

✔ Keep logic in views, not templates
✔ Use template inheritance for layout
✔ Use custom filters/tags for repetitive logic
✔ Organize templates by app (app_name/template_name.html)

Final Thoughts

Django templates are more than HTML. They let you:

  • Build dynamic, interactive pages
  • Reuse layouts efficiently
  • Keep your code clean and maintainable

By mastering templates and context, you can create professional-grade Django applications.

0 Comments