简单的in语句会导致错误

时间:2015-05-12 11:53:03

标签: python django

我想知道,这有什么不对:

{% for x in [1, 2, 3] %}
    <p>{{x}}</p>
{% endfor %}

错误:

'for' statements should use the format 'for x in y': for x in [1, 2, 3]

更新

   # .py
   data = {'key1': .... //dictionary, 'key2': .... // dictionary }

   # template
   {% for k, v in data['key1'].items %}
      <p>{{ k }}</p>
    {% endfor %}

错误:

Could not parse the remainder: '['key1'].items' from 'data['key1'].items'

1 个答案:

答案 0 :(得分:1)

Django模板语言不支持文字列表。要么从视图中传入,要么在字符串上使用make_list过滤器:

{% for x in "123"|make_list %}