简体   繁体   中英

How to iterate over the next element in a tuple in Django?

Say I have a tuple :

a = (1, 2, 3)

Now I want to do something like :

for i in a:
    if i == 1:
        print a[1]

I mean if the first element is 1 I want to immediately extract the second element without going into another iteration of the for loop.

How may I do this in a django template?

This does effectively the same thing as your Python code, assuming that you've passed a to the template context:

{% for i in a %}

    {% if i == 1 %}

        {{ a.1 }}

    {% endif %}

{% endfor %}
{% for i in a %}{% ifequal i 1 %}{{ a.1 }}{% endifequal %}{% 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