Variables - Django Templates

Last Updated : 12 Jun, 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. Key characteristics of Django template 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.

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.

Syntax

{{ variable_name }}

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

Implementation

Create 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

Explanation:

  • Create Context Data: The view stores values in the context dictionary.
  • Render Template: render() passes the context data to geeks.html.
  • Configure URLs: The URL pattern maps requests to the geeks_view() function.
  • Use Template Variables: {{ first_name }} and {{ last_name }} display values from the context dictionary.
  • Generate Output: Django replaces the variables with their corresponding values before sending the HTML response to the browser.

Variable Resolution Order

When Django encounters a variable such as:

{{ user.name }}

It tries to resolve it in the following order:

  1. Dictionary key lookup
  2. Object attribute lookup
  3. List index lookup
  4. Method lookup (without arguments)

This allows a single syntax to work with different types of data.

Comment