[Django]-Get the first element in the for loops in the Django template

16๐Ÿ‘

โœ…

{% for shelf in final_shelf_info %}
    {% if forloop.first %}
        Do something with {{ shelf }} since its the first item iterated
    {% endif %}
{% endfor %}

More on the {% for %} template loop in the docs.

๐Ÿ‘คnik_m

4๐Ÿ‘

You could do something like this:

        {% for t in things %}

                {% if forloop.first %}
                    // do something                            
                {% endif %}

                // do stuff

                {% if forloop.last or things.count == 1 %}
                    // do something
                {% endif %}

        {% endfor %}

More documentation is available at Django documentation

๐Ÿ‘คuser4426017

0๐Ÿ‘

{% if final_shelf_info.0 == shelf %}

or

{% if final_shelf_info.first == shelf %}
๐Ÿ‘คiklinac

Leave a comment