How to Add a For Loop to a Template in Django


In this article, we show how to add a for loop to a template in Django.

So, basically, when we're building a website with Python using Django as the framework, we want to separate the Python code from the HTML code initially.

This is because coding, many times, is a team effort, made of up of a group of software developers/programmers.

Some programmers may know Python. Others may HTML and CSS.

Some may know backend stuff. Others may just like frontend stuff.

So, say, you have a person on your team that is really good with front end things such as HTML and CSS. They design the HTML template for your web page. However, this person does not know Python. So, this software developer may just code the template for, say, your shopping page and leave it at that. So this person created all the HTML code and CSS code. So the frontend is taken care of.

However, this frontend page that has all the HTML and CSS may need some Python hardcoded into it.

So, we have a page such as the page, where we have most of our Python code originally in and in this page, we load a template.

We then go to the template page and we can just add in the Python code that is needed such as for loops, variables, etc.

It's like we have the main template for the web page, such as the Store page, and we still have just add Python on top of it. The Python can then come, take over from the frontend developer, and add in the Python code we need on that page.

So this is what we'll do in this page.

We'll put our main Python code in the page.

We will then load the full complete template that our web page, such as Store page, will run off of.

We then can add the Python code on top of the complete frontend page of HTML and CSS, to form the complete code.

Again, this is the best way because it separates the frontend code (HTML, CSS) from the backend code (Python).

Of course, it's possible for a programmer to know frontend and backend languages. However, many times in major software projects, these are separated. Therefore, you would want them separated in code.

So let's say I created in the file a database table that called Students. In this Students table, I have columns, first name, last name, and email address of each student. I have 2 students in the table. I'm going to import this table into the file and get all the rows of data.

From this, I'm going to load the template of all the HTML code and then I'm going to go to that template page and add the Python code that it needs.

So, those are the steps.

So let's now go to the file. File

So this is the code that we have in the page.

So this page will contain pretty much all of the Python code we need to do all the backend stuff that is needed for the website.

So this page contains our main Python code.

With this code, we import render from django.shortcuts, so that we can render in the template file that we are working with.

We import the database table, Students, that was created in the file of the app.

We then create a function called index with the parameter, request.

In this function, we create a variable named allstudents and set it equal to Students.objects.all(). This line of code retrieves all rows of data from the Students database table.

Realize that you create a folder named template in the App that you are in. In this template folder, you create another folder with the name of the App. In this folder, you place the index.html page.

Realize that you must specify the full path to the template directory in the file in the project directory. You do this in the DIRS list.

We then create a dictionary called context. We assign 'allstudents' to the value of allstudents.

We then use the line, return render(request, 'Articles/index.html', context), to render into the index.html template file the variable context.

This makes sure that we pass into the template the context value and request values.

And this is all that is needed in the file.

We'll now go onto to the index.html file.

Template File- index.html

Before we go on to adding the Python code to the template file, let's first view the template file without any Python added in.

So this is the template code without any Python code added.

It is very basic.

This page yields the following output.

Django template

Now we will add the Python code to this template, so that we can get the data from the Students database table. This Students database holds information on students, including their first name, last name, and email.

This is done through the following code, shown below.

Okay, so we have the same header tag of List of Students.

We then create an ordered list.

Now is where we add the Python data. We add a for loop in the template.

This is done in between {% %}.

We create a for loop, for student in allstudents.

Remember that allstudents from the file was set equal to Students.objects.all(). It obtains all of the objects (student data) from the database table.

So, in this for loop, we go through each record in the Students database table.

We then create an item in the list using the <li> tag.

Variables in Python can be passed to a template using {{ }} with the variable in between.

We print out each student's first name, last name, and email with a colon after the last name.

We then end the for loop block.

And then we end the ordered list.

After running this code, we get the following output, shown below.

Django template with Python for loop

So, that's all that's needed to create a for loop in a template in Django so that you can we can loop through any type of data in Python such as a list or rows in a database table.

Related Resources

How to Randomly Select From or Shuffle a List in Python

HTML Comment Box is loading comments...