How to iterate over nested dictionaries in django templates

后端 未结 3 1420
陌清茗
陌清茗 2020-12-05 02:27

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

3条回答
  •  甜味超标
    2020-12-05 02:50

    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 }} 
    {% for key2,value2 in value.items %} {{ key2 }}
    {% for key3, value3 in value2.items %} {{ key3 }}:{{ value3 }}
    {% 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

提交回复
热议问题