# Jinja2: Loops

Like conditional statements, the Jinja2 templating language also allows you to write loop[1] structures. However, Jinja2 only supports for loops. If you want to use a while loop, you'll need to simulate its behaviour with a for loop.

# Looping Through List Objects

In this section, we'll see how you can add for loop logic to your Jinja2 template. Make a template named for_loop.html and place that in your Flask project's /templates folder. Then, add the following contents to the template:

<!-- templates/for_loop.html -->

<h1>Loops in Jinja2</h1>
<h2>For loop</h2>
<h3>Planets in Our Solar System</h3>

<ul>
    {% for planet in planets %}
        <li>{{ planet }}</li>
    {% endfor %}
</ul>

In the above template, we've traversed through a Python list containing the names of the planets in our solar system and showed them via an unordered HTML list. To render this, create an endpoint called /for-loop/ and add the following code to the app.py file of your Flask project.

# app.py

from flask import Flask, render_template

app = Flask(__name__)


@app.route("/for-loop/")
def render_loops_for():
    planets = [
        "Mercury",
        "Venus",
        "Earth",
        "Mars",
        "Jupyter",
        "Saturn",
        "Uranus",
        "Neptune",
    ]
    return render_template("for-loop.html", planets=planets)

In line 10, notice how we've defined a list object containing the names of the planets and assigned it to a variable planets. Later on, in line 20, we've passed the variable to Flask's render_template method.

Now, if you run the Flask application and head over to http://localhost:5000/for-loop (opens new window) on your browser, you'll see a nicely rendered bullet list showing the names of the planets.

Basic for loop example

# Looping Through Dictionary Objects

Let's explore another example where you'll traverse through the keys and the values of a Python dictionary object and show them in an HTML list. In this case, our HTML template for_loop_dict.html will look like this:

<!-- templates/for_loop_dict.html -->

<h1>Loops in Jinja2</h1>
<h2>For loop</h2>
<h3>World Cuisine</h3>

<ul>
    {% for key, value in cuisines.items() %}
        <li>Country: {{ key }} => Cuisine: {{ value }}</li>
    {% endfor %}
</ul>

In the above template, we've looped through the key-value pairs of a dictionary object called cuisines and showed them in an unordered HTML list. To render this, add the following code to your Flask project's app.py. This will create an endpoint called /for-loop/dict/.

# app.py

from flask import Flask, render_template

app = Flask(__name__)


@app.route("/for-loop/dict/")
def render_loops_for_dict():
    cuisines = {
        "Italy": "Neapolitan Pizza",
        "France": "Baguette",
        "Spain": "Churros",
        "Japan": "Sushi",
        "India": "Dosa",
    }
    return render_template("for_loop_dict.html", cuisines=cuisines)

In line 10, we've defined a dictionary object and assigned it into variable cuisine. Later in line 17, we've passed the dictionary object to the render_template method.

Run the Flask application and head over to http://localhost:5000/for-loop/dict (opens new window) on your browser. You should be able to see an unordered bullet list showing the desired contents.

For loop and dictionary objects

# Mixing Loops & Conditionals

You can combine loops and conditionals to implement more complex logic in Jinja2 template. Let's create another HTML template file named for_loop_conditionals.html and place that in our /template folder. Add the following content to the file:

<!-- templates/for_loop_conditionals.html -->

<h1>Loops in Jinja2</h1>
<h2>For loop & conditional statements</h2>

{% for user, os in user_os %}
    {% if os == "Windows" %}
        <p>{{ user }} is a <b>Windows</b> user</p>

    {% elif os == "MacOS" %}
        <p>{{ user }} is a <b>MacOS</b> user</p>

    {% elif os == "Linux" %}
        <p>{{ user }} is a <b>Linux</b> user</p>
    {% endif %}
{% endfor %}

In the above template, we've looped through a list of tuples where each tuple has two elements. The first element of the tuple denotes a user's name and the second one denotes the name of the operating system that the user uses. We've checked the type of the operating systems via conditional statements and printed different messages accordingly.

To render the template using Flask, add the following code to your project's app.py file:

# app.py

from flask import Flask, render_template

app = Flask(__name__)


@app.route("/for-loop/conditionals/")
def render_for_loop_conditionals():
    user_os = [
        ("Miller", "Windows"),
        ("Bob", "MacOS"),
        ("Zach", "Linux"),
        ("Annie", "Linux"),
        ("Farah", "Windows"),
        ("George", "Windows"),
    ]

    return render_template("for_loop_conditionals.html", user_os=user_os)

In line 10, the variable user_os refers to a list of tuples containing the user's names and their respective operating system of choice. Later, we've passed the variables in the render_template method.

Run the application and head over to http://localhost:5000/for-loop/conditionals (opens new window) on your browser. You should be able to see a page like this:

For loop with conditional statements

# Conclusion

In this lesson, you've learned about using for loops in a Jinja2 template. You've also learned how you can traverse through dictionary objects and combine for loops with conditional statements to implement more complex logic directly in the template.

Enjoyed this article?
You'll love the complete video course!

  • Complete video lessons for each topic
  • Get the most out of your learning
  • Build a portfolio of projects
  • Get one-on-one help from a real person

Get the video course


  1. Loops in Jinja2 Template (opens new window) ↩ī¸Ž