Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django模板变量作为列表的键_Django_Templates_Django Templates - Fatal编程技术网

Django模板变量作为列表的键

Django模板变量作为列表的键,django,templates,django-templates,Django,Templates,Django Templates,我来自这个问题 我在我的视图中创建了以下上下文: {'cats': {'LIST1': ['a','b','c'], 'LIST2': ['aa','bb','cc','dd'], 'LIST3': ['f','g']}} 我想做的是打印列表标题,然后打印所有的项目,比如 LIST1: - a - b - c 对于所有的列表,所以我在我的模板中这样做了 {% for l_name, l in cats %} {{ l_name }} {%for lv in l %}

我来自这个问题

我在我的视图中创建了以下上下文:

{'cats': {'LIST1': ['a','b','c'], 'LIST2': ['aa','bb','cc','dd'], 'LIST3': ['f','g']}}
我想做的是打印列表标题,然后打印所有的项目,比如

LIST1:
- a
- b
- c 
对于所有的列表,所以我在我的模板中这样做了

{% for  l_name, l in cats %}
    {{ l_name }}

    {%for lv in l %}
        {{ lv }}
    {% endfor %}
{% endfor %}
这只打印列表名,而不打印列表。哪里错了


谢谢

如果要迭代键和值,可以使用:

{% for name, lst in cats.iteritems %}
.... 
{% endfor %}
这只是调用dictionary的
iteritems
方法,该方法返回一个2元组列表上的迭代器


也有一些很好的例子。

就记录而言,问题在于如何创建数据。所以不是

{'cats': {'LIST1': ['a','b','c'], 'LIST2': ['aa','bb','cc','dd'], 'LIST3': ['f','g']}}
我需要一份清单,以便:

{'cats': [('LIST1', ['a','b','c']), ('LIST2', ['aa','bb','cc','dd']), ('LIST3', ['f','g'])]}

使用
{{l[lv]}
而不是
{{lv}
?嗯,我认为我不能在模板中使用[]。不管怎样,我发现了问题。我需要用()而不是{}创建touple