如何从django templatetag访问python列表?

时间:2010-01-28 20:20:38

标签: python django templates django-templates yaml

我创建了一个模板标签,将yaml文档加载到python列表中。在我的模板中,我有{% get_content_set %},这会转储原始列表数据。我希望能够做的是像

{% for items in get_content_list %} 
       <h2>{{items.title}}</h2> 
{% endfor %}`

2 个答案:

答案 0 :(得分:3)

如果列表在python变量X中,则将其添加到模板上下文context['X'] = X,然后就可以了

{% for items in X %}
       {{ items.title }}
{% endfor %}

模板标记用于呈现输出,因此不会提供可供使用的可迭代列表。但是你不需要那个,因为正常的上下文+ for循环很好。

答案 1 :(得分:-1)

由于编写复杂的模板标签并不是一项容易的任务(尽管已有详细记录),但我会采用{%with%}标记源并根据我的需要调整它,所以看起来像

{% get_content_list as content %
{% for items in content %} 
       <h2>{{items.title}}</h2> 
{% endfor %}`