How to iterate a list in Django template

We can iterate a list in Django templates by using {% for %} tag. This allow us to display list items on web pages. Django handles the iteration and allows us to access each individual item within the loop, making it easy to create dynamic and data-driven web pages. If the list is empty, we can handle it using the {% empty %} tag. Examples are mentioned below: Example


{% for student in students %}
    {{ student }}
{% endfor %}
Example with empty tag

{% for student in students %}
    {{ student }}
{% empty %}
    No record found.
{% endfor %}