KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm not sure the most efficient way to iterate over my nested dictionaries to print a matrix of the total and good values for every fruit for each date. Take for instance the two lists and dictionary below: fruits = ['apples','oranges','bananas'] harvest_dates = ['2011-07-23','2011-07-22','2011-07-21'] harvest_data = { 'apples': { '2011-07-23': { 'total': 100, 'good': 80}, '2011-07-22': { 'total': 97, 'good': 92}, '2011-07-21': { 'total': 90, 'good': 85} }, 'oranges': { '2011-07-23': { 'total': 86, 'good': 82}, '2011-07-22': { 'total': 90, 'good': 75}, '2011-07-21': { 'total': 92, 'good': 92} }, 'bananas': { '2011-07-23': { 'total': 10, 'good': 9}, '2011-07-22': { 'total': 12, 'good': 11}, '2011-07-21': { 'total': 9, 'good': 9} } } I can easily do this in python: for fruit in fruits: for day in harvest_dates: print "harvest: %s" % harvest_data[fruit][day]['total'] print "good crop: %s" % harvest_data[fruit][day]['good'] But I don't know how to access this data in django templates. I had been trying something such as: {% for fruit in fruits %} ... {% for day in harvest_dates %} ... {{ harvest_data.fruit.day.total }} {{ harvest_data.fruit.day.good }} ... {% endfor %} {% endfor %} But it's simply not working. {% for fruit in fruits %} {{ harvest_data.fruit }} <--- this does not exist {{ harvest_data[fruit] }} <--- this does not work {% endfor %} I'm a complete amateur and I'm probably going about this all wrong, but I've Google'd quite a bit and it's not clear to me what the best approach to getting the data I want is.
Tags (comma-separated)
Save Edits
Cancel