Reputation: 5053
I am having following data:
[{
'mp': [ < MpbMealPlan: MpbMealPlan object > , < MpbMealPlan: MpbMealPlan object > ],
'user': [ < User: mpowner mpowner > ]
}, {
'mp': [ < MpbMealPlan: MpbMealPlan object > , < MpbMealPlan: MpbMealPlan object > ],
'user': [ < User: kvermaOwner Owner > ]
}]
I want to iterate it in my django template. As shown these are two records with 2 keys in each(mp and user) each record belong to particular user. So I wnat to it such that I get mps of user and detail of user. But when I try to iterate it as explained in below answer or any other answer through out SO I every time get bizarre results. mp can contains further multiple records but I am stuck at first iteration only. I am very new to python , it is my 3rd day working. Any guidance would save my day.
As soon as I apply below:
{% for contributor in contributors.details %}
{{ contributor }}
{% endfor %}
and I got this Output which break downs the structure:
{
'mp': [ < MpbMealPlan: MpbMealPlan object > , < MpbMealPlan: MpbMealPlan object > ],
'user': [ < User: mpowner mpowner > ]
} {
'mp': [ < MpbMealPlan: MpbMealPlan object > , < MpbMealPlan: MpbMealPlan object > ],
'user': [ < User: kvermaOwner Owner > ]
}
One query: Is it even possible to get desired results from the data I have. I want to iterate it to get mp and user and then I want to iterate mp to get multiple records within it. As whenever I tried iterating up to any level I get all the records so keys "mp and user" are not solving my purpose.
I don't want to waste any one's time here. I have updated the question. Thanks for answers till now. Update:
Following is the method I used and got desired results:
{% for contributor in contributors.details %}
{% for user in contributor.user %}
{{ user }}
<br>
{% for mp in contributor.mp %}
{{ mp.mp_name }}
<br>
{% endfor %}
{% endfor %}
{% endfor %}
Results:
user:mpowner mpowner
mp:Fresh & Healthy
mp:evening snacks
user:kvermaOwner Owner
mp:Fresh & Healthy
mp:Energizing
At last I got desired output with C14L's help (Amazing guidance). Thanks to "ajabdelaziz" and others too.
Upvotes: 0
Views: 214
Reputation: 12558
For Python3, calling .items()
gives you the items.
for key, item in data.items():
For Python2 use iteritems()
:
for k, v in knights.iteritems():
In Django template:
<div>
{% for k,v in test.items %}
<p>{{ k }} --> {{ v }}</p>
{% endfor %}
</div>
Docs: https://docs.python.org/3/tutorial/datastructures.html#looping-techniques
Edit:
To add to the answer, looking at your specific object
[('details', [{
'mp': u '[{"fields": {"status":
look at this part: 'mp': u '[{"fields": {"st
That is not a list()
, it is a string: u''
When you loop a string, you get the individual characters the string contains, one by one. Hence the "strange" result.
Edit 2:
Both contributor.mp
and contributor.user
contain lists
{% for contributor in contributors.details %}
{{ contributor.mp }}
{{ contributor.user }}
{% endfor %}
so, for example to print all usernames, you can do
{% for contributor in contributors.details %}
{% for user in contributor.user %}
{{ user.username }}
{% endfor %}
{% endfor %}
To print the list of mp
items, you can do
{% for contributor in contributors.details %}
{% for mp in contributor.mp %}
{{ mp }}
{% endfor %}
{% endfor %}
But each mp
object has probably a number of attributes. You need to look up what your MpbMealPlan
class definition looks like.
Upvotes: 1
Reputation: 132
From what I'm seeing based on your error, is that you're trying to iterate over mp which has a value with dictionaries and lists in them. Because of the different data types, you cannot just use .items()
only. You'll need to use .items()
to get the mp's value in key,value. then you'll have to deal with iterating over the list then the key, values again. C14L's answer shows how to do that in templates just don't forget about your datatypes!
i.e -
<div>
{% for k,v in test.items %}
{% for item in v%}
{% item %}
{% endfor %}
{% endfor %}
</div>
item would be the list that you can then iterate over to get additional key value pairs.
Another possible solution is to do most of this in a template tag filter. and then just apply the filter onto the selected variable you want it to filter out.
Upvotes: 1