我的視圖代碼基本上是這樣的:在Django模板中訪問並行數組?
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}}。 – slacy 2010-01-14 19:54:07