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:
我可以在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:
但我不知道如何在django模板中访问这些数据。我一直在尝试这样的事情:
{% 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.
我是一个完全的业余爱好者,我可能会说这一切都错了,但我已经谷歌了很多,我不清楚获得我想要的数据的最佳方法是什么。
3 个解决方案
#1
30
Since you're familiar with python, the following is logically how you would want to iterate through your dictionary in a Django template:
由于您熟悉python,以下是逻辑上您希望如何在Django模板中迭代字典:
for key,value in harvest_data.items():
... print key
... for key2,value2 in value.items():
... print key2
... for key3,value3 in value2.items():
... print "%s:%s"%(key3,value3)
In your template, this translates as follows:
在您的模板中,这翻译如下:
{% for key, value in harvest_data.items %}
{{ key }} <br>
{% for key2,value2 in value.items %}
{{ key2 }} <br>
{% for key3, value3 in value2.items %}
{{ key3 }}:{{ value3 }} <br>
{% endfor %}
{% endfor %}
{% endfor %}
The Django docs actually briefly include an example of how to iterate through dictionaries when describing how the for
template tag works:
Django文档实际上简要地包含了一个示例,说明在描述for template标签的工作原理时如何遍历字典:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#for
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#for
#2
7
as rolling stone says thats the way to iterate over dictionaries in templates, i would only change the key, value keywords for different keywords in every iteration like this:
作为滚石说这是在模板中迭代字典的方式,我只会在每次迭代中更改不同关键字的键值关键字,如下所示:
{% for key, value in harvest_data.items %}
{{ key }} <br>
{% for key2,value2 in value.items %}
{{ key2 }} <br>
{% for key3, value3 in value2.items %}
{{ key3 }}:{{ value3 }} <br>
{% endfor %}
{% endfor %}
{% endfor %}
just for the sake of clarity :)
只是为了清楚:)
And if you want to line up your values i would suggest you use another data structure where you can sort by date, for example a something like this:
如果你想排列你的值,我建议你使用另一个数据结构,你可以按日期排序,例如这样的事情:
{ 'oranges' : [(date1, value1), (date2,value2)] ...}
Try to do the least possible operations in your templates, so dont do a sort or nested if's if you dont have to
尝试在模板中进行尽可能少的操作,因此如果不需要,不要进行排序或嵌套
#3
1
Really old question, but I will add my 1.5c.
真的很老问题,但我会加上我的1.5c。
This is a good use case of the regroup tag (https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#regroup) and a bit of data refactoring:
这是regroup标记(https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#regroup)的一个很好的用例和一些数据重构:
-
Have your data as a simple list of data points:
将您的数据作为简单的数据点列表:
harvest_data = [ {'fruits': 'apples', 'date': '2011-07-23', 'total': 100, 'good': 80}, # ... ]
-
In your template, group by the chosen dimension(s):
在您的模板中,按所选维度进行分组:
{% regroup harvest_data by fruits as data_by_fruits %} {% for data in data_by_fruits %} <h1>{{ data.grouping }}</h1> # 'apples' {% regroup data.list by date as data_by_fruits_date %} {% for data_1 in data_by_fruits_date %} <h2>{{ data_1.grouping }}</h2> # '2011-07-23' {% for datapoint in data_1.list %} total: {{ datapoint.total }} <br/> good: {{ datapoint.good }} <br/> {% endfor %} {% endfor %} {% endfor %}
#1
30
Since you're familiar with python, the following is logically how you would want to iterate through your dictionary in a Django template:
由于您熟悉python,以下是逻辑上您希望如何在Django模板中迭代字典:
for key,value in harvest_data.items():
... print key
... for key2,value2 in value.items():
... print key2
... for key3,value3 in value2.items():
... print "%s:%s"%(key3,value3)
In your template, this translates as follows:
在您的模板中,这翻译如下:
{% for key, value in harvest_data.items %}
{{ key }} <br>
{% for key2,value2 in value.items %}
{{ key2 }} <br>
{% for key3, value3 in value2.items %}
{{ key3 }}:{{ value3 }} <br>
{% endfor %}
{% endfor %}
{% endfor %}
The Django docs actually briefly include an example of how to iterate through dictionaries when describing how the for
template tag works:
Django文档实际上简要地包含了一个示例,说明在描述for template标签的工作原理时如何遍历字典:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#for
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#for
#2
7
as rolling stone says thats the way to iterate over dictionaries in templates, i would only change the key, value keywords for different keywords in every iteration like this:
作为滚石说这是在模板中迭代字典的方式,我只会在每次迭代中更改不同关键字的键值关键字,如下所示:
{% for key, value in harvest_data.items %}
{{ key }} <br>
{% for key2,value2 in value.items %}
{{ key2 }} <br>
{% for key3, value3 in value2.items %}
{{ key3 }}:{{ value3 }} <br>
{% endfor %}
{% endfor %}
{% endfor %}
just for the sake of clarity :)
只是为了清楚:)
And if you want to line up your values i would suggest you use another data structure where you can sort by date, for example a something like this:
如果你想排列你的值,我建议你使用另一个数据结构,你可以按日期排序,例如这样的事情:
{ 'oranges' : [(date1, value1), (date2,value2)] ...}
Try to do the least possible operations in your templates, so dont do a sort or nested if's if you dont have to
尝试在模板中进行尽可能少的操作,因此如果不需要,不要进行排序或嵌套
#3
1
Really old question, but I will add my 1.5c.
真的很老问题,但我会加上我的1.5c。
This is a good use case of the regroup tag (https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#regroup) and a bit of data refactoring:
这是regroup标记(https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#regroup)的一个很好的用例和一些数据重构:
-
Have your data as a simple list of data points:
将您的数据作为简单的数据点列表:
harvest_data = [ {'fruits': 'apples', 'date': '2011-07-23', 'total': 100, 'good': 80}, # ... ]
-
In your template, group by the chosen dimension(s):
在您的模板中,按所选维度进行分组:
{% regroup harvest_data by fruits as data_by_fruits %} {% for data in data_by_fruits %} <h1>{{ data.grouping }}</h1> # 'apples' {% regroup data.list by date as data_by_fruits_date %} {% for data_1 in data_by_fruits_date %} <h2>{{ data_1.grouping }}</h2> # '2011-07-23' {% for datapoint in data_1.list %} total: {{ datapoint.total }} <br/> good: {{ datapoint.good }} <br/> {% endfor %} {% endfor %} {% endfor %}