【发布时间】:2011-01-05 05:44:51
【问题描述】:
我的视图代码基本上是这样的:
context = Context()
context['some_values'] = ['a', 'b', 'c', 'd', 'e', 'f']
context['other_values'] = [4, 8, 15, 16, 23, 42]
我希望我的模板代码如下所示:
{% for some in some_values %}
{% with index as forloop.counter0 %}
{{ some }} : {{ other_values.index }} <br/>
{% endwith %}
{% endfor %}
我希望这个输出:
a : 4 <br/>
b : 8 <br/>
c : 15 <br/>
d : 16 <br/>
e : 23 <br/>
f : 42 <br/>
这可能吗?我发现我的“with”语句实际上正在工作,但是使用该变量作为参考是行不通的。我怀疑对于 {{ other_values.index }} 它正在执行 other_values['index'] 而不是 other_values[index]。这可能吗?
【问题讨论】:
-
我总是可以为这个用例编写一个自定义模板标签,但这似乎有点矫枉过正。我不想说{{ other_values|access:index }}。
标签: python django django-templates