How to Pass a Python Variable to a Template in Django


In this article, we show how to pass a Python variable to a template in Django.

Basically, this is it. You have the part in your Django package, where you keep your main Python code. Normally, this would be the (for database connection) and (for variables and variable manipulation) and such.

Outside of this, you have a template or templates, where you have your frontend stuff, such as your HTML file that forms the frontend of your website, what the user sees.

Django allows for separation of Python code (backend code) with frontend code (such as HTML, CSS, and Javascript).

This way, frontend sofware developers can work on frontend things, like backend developers can focus on backend things.

However, the frontend files (HTML template files) may need Python code added to it from the backend code (found in or So, it is important to know how to add Python code to a frontend file in Django.

So, basically you do all your backend work in the and files and then at the end, a backend engineer can just add the code to finished frontend file that a frontend engineer has made.

How do you pass a Python variable to a template?

And this is rather simple, because Django has built-in template modules that makes a transfer easy.

Basically you just take the variable from and enclose it within curly braces {{ }} in the template file.

We won't go into a database example for this one. We'll do a very simple example in the file and then transfer to the template we are working with.

This will pass the Python variable to a template in Django. File

So, in this code, we create a Python variable called Craig.

We will then pass this into the template file we are dealing with Greeting.html.

You'll see exactly what's going on.

The code for this file that we have is shown below.

So now we'll go over each line of code.

First, we must import render from django.shortcuts, so that we can render in the template file that we are working with.

Next, we create a function called index that always must have a request parameter.

We create a dictionary named person that has attributes firstname and lastname. Craig is the person's first name and Daniels is the person's last name.

We also create a variable called weather and set it equal to "sunny".

We then create a dictionary named context. In this dictionary context, we pass every variable into that we want passed to the template file.

Context is a special dictionary that holds every variable that you want passed into the template file that you are working with.

In our line of code, we render into the template context and request. So, basically, we pass in the person and weather variables to the greetings.html template file.

This concludes the file.

Template File- greetings.html

So, next, we have our template file, which is greetings.html.

In this file, we keep it very basic. We just want to focus on how to display the variables that we had present in the file.

So, this is the composition of the file.

So, this file is very simple.

In h1 header tags, we say Hi, followed by {{ person.firstname }} {{ person.lastname }} followed by closing the header tags.

So, remember to display variables in Django, you use double opening and closing curly braces, {{ }}. Inside of these curly braces, you specify the variable that you want to display. So, we created a dictionary named person that contains the person's first and last name. Therefore, we call the first name, by person.firstname and the last name by person.lastname.

Next, we create another header tag and say "Today it is " followed by the variable weather. Remember weather was set to "sunny". Therefore, in this line we are saying, Today it is sunny.

And this is all that is needed to pass Python variables to a template in Django.

Running this code, I get the following output shown below.

Django template with Python variables passed to it

You can see that we've successfully passed the Python variables from the file to the template, greetings.html.

So, you can see that transferring from Python code to templates is very simple.

Related Resources

How to Randomly Select From or Shuffle a List in Python

HTML Comment Box is loading comments...