Variables - Django Templates

Last Updated : 13 Apr, 2026

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'),
]

Configure URL Routing to the main project:

Python
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('geeks.urls')),
]

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.

Note: If using app-level templates, ensure APP_DIRS=True is set in settings.py so Django can automatically locate templates inside app directories.

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
Comment