Indradhanush Gupta
Indradhanush Gupta

Reputation: 4237

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?

Upvotes: 0

Views: 129

Answers (2)

jdotjdot
jdotjdot

Reputation: 17052

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 %}

Upvotes: 2

Brent Washburne
Brent Washburne

Reputation: 13158

{% for i in a %}{% ifequal i 1 %}{{ a.1 }}{% endifequal %}{% endfor %}

Upvotes: 0

Related Questions