alias51
alias51

Reputation: 8648

How to select first item in a list

I have the following list:

{% for upcomming_gig in upcomming_gigs %}
{% with gig=upcomming_gig.gig %}

...

{% endfor %}
{% endif %}

This ends up printing the entire list of gig records. How can I select just the first in the list?

I have tried {% with gig=upcomming_gig.gig.0 %} as per https://stackoverflow.com/a/26144595/2429989 but that results in no data...?

Upvotes: 4

Views: 5697

Answers (1)

Sourabh
Sourabh

Reputation: 8502

It'll be best to do these kind of things on the server side but I guess (didn't try) this will work for django, don't know anything about Jinja

{% for upcomming_gig in upcomming_gigs %}
    {% if forloop.first %}
    {# or loop.first for Jinja as Ilendi mentioned below #}
        ...
    {% endif %}
{% endfor %}

>> More variables >>

EDIT

I just saw the question you linked to, you're writing using upcomming_gig instead of upcomming_gigs (notice the s in the end). Try this:

{% if upcomming_gigs %}
    {% with gig=upcomming_gigs.0.gig %} # Instead of gig=upcomming_gigs.gig.0
        ...
    {% endwith %}
{% endif %}

Upvotes: 6

Related Questions