Thanks to visit codestin.com
Credit goes to www.geeksforgeeks.org

Open In App

Variables - Django Templates

Last Updated : 30 Oct, 2025
Comments
Improve
Suggest changes
8 Likes
Like
Report

Django templates allow HTML to be dynamically generated by combining static content with data passed from views. Variables are used to display this dynamic data within templates. Important things about variables are:

  • Variables come from the context dictionary provided by the view.
  • They render values directly into HTML.
  • Can display dynamic content such as strings, numbers, lists, or objects.
  • Enable separation of presentation from business logic.

Syntax

{{ variable_name }}

Example: Passing a context with the following data

{
'first_name': 'Rahul',
'last_name': 'Kumar'
}

And in template:

My first name is {{ first_name }}.
My last name is {{ last_name }}.

Output:

My first name is Rahul.
My last name is Kumar.

Django Template Language (DTL) allows dynamic data from views to be rendered directly within HTML templates.

Implementation Example

Consider a project called 'geeksforgeeks' with an app named 'geeks':

1. Create a View with Context Data

In geeks/views.py:

Python
from django.shortcuts import render

def geeks_view(request):
    context = {
        "first_name": "Prajjwal",
        "last_name": "Vishwakarma",
    }
    return render(request, "geeks.html", context)

2. Configure URL Routing

In geeks/urls.py:

Python
from django.urls import path
from . import views

urlpatterns = [
    path('',views.geeks_view, name = 'geeks_view'),
]

3. Create the Template

Create a file named geeks.html inside the app’s templates folder. If the folder doesn’t exist, create it first. All HTML files for the app should be placed in this folder.

html
My First Name is {{ first_name }}.
<br/>
My Last Name is  {{ last_name }}.

Run the development server using python manage.py runserver and Visit: http://127.0.0.1:8000/:

django01
Snapshot of the development server

Explore