Hi there, we’re Harisystems

"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".

For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.

1
1

Django for Loop: Iterating Over Collections in Templates

Introduction

Django provides a powerful template language that includes a for loop construct. With the for loop, you can iterate over collections, such as lists, querysets, or dictionaries, and perform operations on each item. In this guide, we will explore how to use the for loop in Django templates with examples.

Step 1: Basic for Loop

The for loop allows you to iterate over a collection and execute a block of code for each item. Here's an example:


<ul>
{% for item in my_list %}
    <li>{{ item }}</li>
{% endfor %}
</ul>
    

In this example, we use the {% for %} and {% endfor %} tags to define the for loop. The loop iterates over the my_list collection, and for each item, it outputs a list item element containing the value of the item.

Step 2: Accessing Loop Variables

Within the for loop, you have access to loop variables that provide additional information about the iteration. Here's an example:


{% for item in my_list %}
    <p>{{ forloop.counter }}. {{ item }}</p>
{% endfor %}
    

In this example, we use the {{ forloop.counter }} variable to display the current iteration count. The output will be a paragraph element with the iteration count followed by the value of each item in the my_list collection.

Step 3: Nested for Loops

You can also nest for loops to iterate over multiple collections simultaneously. Here's an example:


{% for category in categories %}
    <h2>{{ category.name }}</h2>
    <ul>
    {% for item in category.items %}
        <li>{{ item.name }}: ${{ item.price }}</li>
    {% endfor %}
    </ul>
{% endfor %}
    

In this example, we iterate over a collection of categories. Within each category, we iterate over a nested collection of items. The output will be a hierarchical structure with headings for each category and a list of items within each category.

Conclusion

The for loop in Django templates provides a powerful way to iterate over collections and perform operations on each item. By following this guide, you have learned how to use the for loop in Django templates, access loop variables, and even nest for loops to handle more complex scenarios. With the flexibility of the for loop, you can create dynamic and data-driven templates in your Django web applications.

4.5L

Learners

20+

Instructors

50+

Courses

6.0L

Course enrollments

4.5/5.0 5(Based on 4265 ratings)

Future Trending Courses

When selecting, a course, Here are a few areas that are expected to be in demand in the future:.

Beginner

The Python Course: Absolute Beginners for strong Fundamentals

By: Sekhar Metla
4.5 (13,245)
Intermediate

JavaScript Masterclass for Beginner to Expert: Bootcamp

By: Sekhar Metla
4.5 (9,300)
Intermediate

Python Coding Intermediate: OOPs, Classes, and Methods

By: Sekhar Metla
(11,145)
Intermediate

Microsoft: SQL Server Bootcamp 2023: Go from Zero to Hero

By: Sekhar Metla
4.5 (7,700)
Excel course

Future Learning for all

If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.

View Courses

Most Popular Course topics

These are the most popular course topics among Software Courses for learners