简体   繁体   中英

accessing a list of tuples in html

I have a list of tuples called top_5 which are the top 5 users usernames and their corresponding post count.

This is a list that has 5 items and each item has 2 indexes.

{% for user in top_5 %}
   {{ user }}
{% endfor %}

returns the user and his post count but is there a way I can get them separately?

Is this a Django template? You can access index values in Django template using the dot notation:

{% for user in top_5 %}
    {{ user.0 }} {{ user.1 }}
{% endfor %}

And as mentioned in the comments, unpacking the tuples is also an option:

{% for username, post_count in top_5 %}
    {{ username }} {{ post_count }}
{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM