【发布时间】:2012-11-02 19:35:45
【问题描述】:
如何在 django 模板中使用变量作为索引?
现在我收到此错误:
Exception Type:
TemplateSyntaxError
Exception Value:
Could not parse the remainder: '[year]' from 'bikesProfit.[year]'
我也尝试过{{ bikesProfit.year }},但结果为空。
{% for year in years_list %}
<tr>
<th>Total profit {{ year }}:</th>
</tr>
<tr>
<th></th>
<th> {{ bikesProfit[year] }} </th>
...
【问题讨论】:
-
如果bikesProfit 是一个以
{2012:10.0, 2011:13.0}格式传递给上下文的字典,那么bikesProfit.year应该可以工作。您可能会在一年中混合使用str和int键吗? -
将
bikesProfit和years_list传递给上下文时它们会是什么样子? -
你试过那个 RickyA 吗?在我看来,它不应该起作用,因为密钥不是
year,而是实际年份(2000 年)。 -
现在试了,确实不行。它会做 bikesProfit["year"]。呸。
标签: python django arrays templates indexing