alias51
alias51

Reputation: 8648

How to ignore the first item in a list in a FOR function?

I have the following for loop that spits out all photos in a list:

{% if photos %}
{% for photo in photos %}
    {% thumbnail photo.photo "100x100" crop="center" as im %}
    <img src="{{ im.url }}" alt="User's photos" data-ajax="{% url 'photo_increase_view' pk=photo.id %}"/>
    {% endthumbnail %}
{% endfor %}
{% endif %}

How can edit this to ignore the first result in the list (i.e. display items 2, 3, 4... etc)

Upvotes: 2

Views: 2055

Answers (3)

michaelb
michaelb

Reputation: 747

Check out forloop.first using Django; for example:

{% if photos %}

    {% for photo in photos %}
        {% if not forloop.first %}
            {% thumbnail photo.photo "100x100" crop="center" as im %}
            <img src="{{ im.url }}" alt="User's photos" data-ajax="{% url 'photo_increase_view' pk=photo.id %}"/>
            {% endthumbnail %}
        {% endif %}
    {% endfor %}

{% endif %}

Upvotes: 4

aitorhh
aitorhh

Reputation: 2451

I guess the answer it's a bit late. But a solution is:

{% for photo in photos[1:] %}
.....
{% endfor %}

Upvotes: 4

g4ur4v
g4ur4v

Reputation: 3288

Use slice

Replace

{% for photo in photos %}

by

{% for photo in photos|slice:"1:" %}

So , complete code

{% if photos %}
{% for photo in photos|slice:"1:" %}
    {% thumbnail photo.photo "100x100" crop="center" as im %}
    <img src="{{ im.url }}" alt="User's photos" data-ajax="{% url 'photo_increase_view' pk=photo.id %}"/>
    {% endthumbnail %}
{% endfor %}
{% endif %}

Upvotes: 5

Related Questions