Django has Template System for Display Content on Web. Using Views and Django Template we can display data in the template. There are lots of Django Template Tags and Filter for displaying data dynamically. We can pass the Model data to view for rendering it in the template.

In this tutorial, you will learn how to display data in templates. How to use different template tags and filter for dynamic render data. Display data from the model/database and display it. How to pass variables and context from views to the Django template system.

How to Pass Context from View to Template

We will be using generic views for passing data to a template. You can different generic views like ListView, DetailView, View, etc. And with those generics views, you can use different methods like get, get_context_data, get_queryset.

In this example, I am using ListView to display Post Model Data. I am passing the object name as post to blog.html. We can access the context using the pass object name.


from django.views import generic
from .models import Post

class PostView(generic.ListView):
    template_name = 'blog.html'
    model = Post
    context_object_name = "post"

from django.contrib import admin
from django.urls import path
from blog_app.views import PostView

urlpatterns = [
    path('', PostView.as_view(), name='post'),

How to Render Variable and Context in Django Template

Django template dynamic variable name. Now that we have passed the context, we need to display that data. We will be using for loop in the template as it is queryset.

{% for blog in post %}
{% endfor %}

Notepost_title, post_body, post_author, post_date are fields in Post Model.

For example, our template (blog.html) will look like this –

{% extends 'base.html' %}

{% load static %}

{% block content %}

<div class="col-sm-8">
    {% for blog in post %}
        <small>Posted by {{blog.post_author}} on {{blog.post_date}}</small>
        <hr />
    {% endfor %}

{% endblock content %}

You can learn more about Built-in Tags and Built-in Filters.

Django Template Expert

To Become Django Template Expert Follow my Django Template Series

  1. How to Integrate Bootstrap 4 Template in Django
  2. How to write Django Custom Template Tags and Filters

GitHub – Run Example Locally

It is also available on GitHub –

Clone the Repository –

git clone

Change Directory

cd How-to-Render-Dynamic-Data-in-Django-Template

Create Virtual Environment – VirtualENV

virtualenv env

Activate Virtual Environment

source env/bin/activate

Run requirement.txt file to install libraries using Pip3

pip3 install -r requirements.txt

Run the server

python3 runserver

And open http://localhost:8000/ in your browser.

You have learned to – pass data to template django, django pass data to template, django views render template, django create table dynamically, display data from database in django