Reputation: 317
I'm trying to iterate list in django template. In python it looks like below and it works:
l=[lots ot touples]
for li in range(len(l)):
print(l[li])
but in django template it does not work. My question is : what is simpliest equivalent of iterating list in django template ?
My django template:
<html>
<body>
{% for w in l %}
<li>{{ w }}</li>
{% endfor %}
</body>
</html>
Thank you in advance
P.S. I have added touples to list to show exactly what is the problem Another one PS
Output of www site ( i'm displaying unread emails form gmail) looks like below:
('From1', 'subject1', 'Thu, 12 Nov 2015 09:46:34 +0100')
('From2', 'subject2', 'Thu, 12 Nov 2015 13:48:58 +0100')
('From3', 'subject3', 'Thu, 12 Nov 2015 14:12:34 +0100')
I want every item in list assign to touple. What i'm trying to do is to assign touple , for instance:
touple1 = ('From1', 'subject1', 'Thu, 12 Nov 2015 09:46:34 +0100')
so i can later call 'from', 'subject' or 'date'
regards
Maybe i will show it in another way to make it more clear: for loop to iterate through the email messages (list of touples )
email1 = list[1] //touple
email2 = list[2] //touple
(...)
from1 = email1[0]
subject1 = email1[1]
date1 = email1[3]
and so on.
Solved. Thank you all for help. Every solution was for me helpfull. I would like to mark two answers ( if it possbile ) as a very usefull for me showing different way to solve same problem. I would like to mention that every help was very usefull for me. Do you mind if i will mark as correct answers Alasdair and Noah answer ( dunno how many I can mark) ?
Upvotes: 2
Views: 7017
Reputation: 309129
Given the following list of tuples:
emails = [
('From1', 'subject1', 'Thu, 12 Nov 2015 09:46:34 +0100')
('From2', 'subject2', 'Thu, 12 Nov 2015 13:48:58 +0100')
('From3', 'subject3', 'Thu, 12 Nov 2015 14:12:34 +0100')
]
You can loop through the tuples, and use .0
or .2
to access the elements by index:
{% for email in emails %}
{{ email.0 }} - {{ email.1 }} - {{ email.2 }}
{% endfor %}
Or, you can unpack the tuple variables:
{% for from, subject, date in emails %}
{{ from }} - {{ subject }} - {{ date }}
{% endfor %}
As an aside, your Python code is not very pythonic. Instead of looping over a range,
emails = [...]
for li in range(len(emails)):
print(emails[li])
you can loop over the list directly
for email in emails:
print(emails)
You can do the same unpacking as in the Django template.
for from, sender, date in emails:
print(from, sender, date)
Upvotes: 6
Reputation: 1731
Your solution looks correct. You have structured it correctly as shown in the docs: https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#for
However, it is possible that the list that you are referencing is not in the context of the template so the template is unable to see it. Make sure to add the list to the context: https://docs.djangoproject.com/en/1.8/ref/templates/api/#rendering-a-context
Given that your list contains tuples you need to access each element of the tuple. If your tuples are identical and contain exactly three items each you can unpack them:
<html>
<body>
{% for from, subject, date in l %}
<li>{{ from }}, {{ subject }}, {{ date }}</li>
{% endfor %}
</body>
</html>
If the tuples are not the same length you may want to do some preprocessing on the list before sending it to the template. You could create an object and convert the tuple to an object with named fields.
class Email(object):
def __init__(self, email_tup):
self.from = email_tup[0]
self.subject = email_tup[1]
self.date = email_tup[2]
# do more complex logic here for varying sized tuples
if len(email_tup) > 3:
self.body = email_tup[3]
email_list = [Email(w) for w in l]
Then pass the new list to the template context.
<html>
<body>
{% for email in email_list %}
<li>{{ email.from }}, {{ email.subject }}, {{ email.date }}</li>
{% endfor %}
</body>
</html>
Upvotes: 1
Reputation: 10119
There's no need to use range
with len
to do that like you do in C or JavaScript, just iterate over it:
l=[some array elements]
for item in l:
print(item)
In your template:
<html>
<body>
{% for item in l %}
<li>{{ item }}</li>
{% endfor %}
</body>
</html>
If l
is a queryset:
<html>
<body>
{% for item in l %}
<li>{{ item.field1 }}</li>
<li>{{ item.field2 }}</li>
{% endfor %}
</body>
</html>
Edit:
l=[lots ot touples]
If the tuple has only two items, stick to @danielfranca's answer:
{% for item1, item2 in items_list %}
....
{% endfor %}
If the tuple's lengths are variable, then you are gonna have to iterate again:
{% for tup in items_list %}
{% for item in tup %}
<li>{{ item }}</li>
{% endfor %}
{% endfor %}
Upvotes: 2
Reputation: 5322
You can do a very similar thing: https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#for-empty
<ul>
{% for athlete in athlete_list %}
<li>{{ athlete.name }}</li>
{% empty %}
<li>Sorry, no athletes in this list.</li>
{% endfor %}
</ul>
Check if your list is what you are expecting, maybe it's missing in the context, or if there's some other error being raised somewhere else.
Maybe it's a dictionary? In that case you would need something like this:
{% for key, value in data.items %}
{{ key }}: {{ value }}
{% endfor %}
If it's a tuple you can unpack the values into multiple variables
{% for item1, item2 in items_list %}
Upvotes: 4