Reputation: 590
My problem is that I want to read a list (with a for loop) which is inside a tuple like this:
items = {a, b, [1, 2, 3]}
'a' and 'b' are other data I need.
Now for reading a list I do this, which doesn't work:
{% for item in items.3 %}
{{item}}
{% endfor %}
So my question is how can I read, with a for loop, a list which is inside a tuple?
Thanks for the help.
Upvotes: 0
Views: 138
Reputation: 590
I just deleted the part of code and started over. An extra tip: If the list in the tuple is a list of tuples and you want to read a part of the tuple. You have to use a new for loop to read it.
For example, I have this:
items = {a, b, [{1, 2, 3}, {4, 5, 6}]}
Now for getting a list out of a tuple:
{% for item in items.3 %}
{{item}}
{% endfor %}
Now for getting something out of the tuple from the list
I try to do this but it doesn't work, if there are people who know how to fix this, please add a comment:
{% for item in items %}
{{item.1}}
{% endfor %}
This is my trick to get it done:
{% for item in items %}
{% for i in item %}
{{i}}
{% endfor %}
{% endfor %}
To get the counter of the outer most loop check this post: how-to-access-outermost-forloop-counter-with-nested-for-loops-in-django-template
Thanks
Upvotes: 0
Reputation: 45575
Python indexes in lists/tuples are started from zero. So you should use the index 2
:
{% for item in items.2 %}
{{ item }}
{% endfor %}
BTW, tuples are defined with round brackets, not the curly:
items = (a, b, [1, 2, 3])
Upvotes: 2