django template and dictionary of lists

强颜欢笑 提交于 2019-12-03 12:19:49

I supose that you are looking for a nested loop. In external loop you do something with dictionary key and, in nested loop, you iterate over iterable dictionary value, a list in your case.

In this case, this is the control flow that you need:

{% for key, value_list  in example_dictionary.items %}
  # stuff here (1)
  {% for value in value_list %}
    # more stuff here (2)
  {% endfor %}
{% endfor %}

A sample:

#view to template ctx:
example_dictionary = {'a' : [1,2]}

#template:
{% for key, value_list  in example_dictionary.items %}
  The key is {{key}}
  {% for value in value_list %}
    The key is {{key}} and the value is {{value}}
  {% endfor %}
{% endfor %}

Results will be:

'a'
1
2

If this is not that you are looking for, please, use a sample to ilustrate your needs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!