Open In App

variables - Django Templates

Last Updated : 14 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Prerequisite- Django Templates

In Django, templates are used to dynamically generate HTML content by combining static HTML with dynamic data from views. One of the simplest and most useful features of Django templates is the use of variables. Variables allow you to display data passed from a view inside your HTML.

Syntax

{{ variable_name }}

Example: Suppose we pass a context with the following data

{

'first_name': 'Naveen',

'last_name': 'Arora'

}

And in your template:

My first name is {{ first_name }}.

My last name is {{ last_name }}.

Output:

My first name is Naveen.

My last name is Arora.

This is how Django lets you inject dynamic content directly into your HTML templates using the Django Template Language (DTL).

Implementation Example

1. Create a Django Project and App

If the project and app is alreday created, proceed to further steps and if not then refer to the following article to learn hoe to create and setup:

Refer to the following articles to check how to create a project and an app in Django.  

Assume your project is named geeksforgeeks and your app is named geeks.

2. Create a View with Context Data

Add this code in geeks/views.py:

Python
from django.shortcuts import render

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

3. Configure URL Routing

In geeks/urls.py add the following code to link the view:

Python
from django.urls import path
from . import views

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

4. Create the Template

Create a file named geeks.html inside a templates folder of the app, if there isn't a templates folder then create one and then create the required html files in it (all the html files will be served to the app from this folder).

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

To check if the app is working, run the app using command- python manage.py runserver and visit the development URL - http://127.0.0.1:8000/

django01
Snapshot of the development server

Next Article
Practice Tags :

Similar Reads