如何迭代django模板中的嵌套字典 [英] How to iterate over nested dictionaries in django templates

查看:524
本文介绍了如何迭代django模板中的嵌套字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定迭代我的嵌套字典的最有效的方式来打印每个日期的每个水果的总计和良好值的矩阵。例如下面的两个列表和字典:

  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':{
'总数:10,
'好':9},
'2011-07-22':{
'total':12,
'go od':11},
'2011-07-21':{
'total':9,
'good':9}
}
}

我可以轻松地在python中执行此操作:



<$水果中的pre code
收获日:
打印收获:%s%harvest_data [fruit] [day] ['total']
打印好的作物:%s%harvest_data [fruit] [day] ['good']

但是我不知道如何在django模板中访问这些数据。我一直在尝试这样的东西:

  {%果实水果%} 
...
{%for harvest_dates%}
...
{{harvest_data.fruit.day.total}}
{{harvest_data.fruit.day.good}}
...
{%endfor%}
{%endfor%}

但是它根本不起作用。

  {%for fruit in fruits%} 
{{harvest_data.fruit}}< ; ---这不存在
{{harvest_data [fruit]}}< ---这不工作
{%endfor%}
/ pre>

我是一个完整的业余爱好者,我可能会这样做都错了,但我已经相当了Google了,我不清楚获取我想要的数据的最佳方法是。

解决方案

由于您熟悉python,以下是在逻辑上如何你想要在Django模板中迭代你的字典:

  e在harvest_data.items()中:
...打印键
...对于key2,value2在value.items()中:
... print key2
..对于key3,value2在value2.items()中:
... print%s:%s%(key3,value3)

在您的模板中,翻译如下:

  {%在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%}

Django文档实际上简要地介绍了如何在描述模板标签的如何运行时如何迭代字典:



https://docs.djangoproject.com / en / dev / ref / templates / builtins /#for


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.

解决方案

Since you're familiar with python, the following is logically how you would want to iterate through your dictionary in a Django template:

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:

https://docs.djangoproject.com/en/dev/ref/templates/builtins/#for

这篇关于如何迭代django模板中的嵌套字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆