2010-06-09 55 views
2

我有一个字典number_devices我传递给模板,字典键是对象列表的ID,我也传递给模板(称为implementations )。我遍历对象的列表,然后尝试使用object.id得到一个值从字典中,像这样:模板中的Django字典:从另一个对象属性获取密钥

{% for implementation in implementations %} 
     {{ number_devices.implementation.id }} 
    {% endfor %} 

不幸的是number_devices.implementation先评估,然后在result.id显然是评估和恢复什么都没显示我不能使用括号:

{{ number_devices.(implementation.id) }} 

因为我得到一个分析错误。如何在Django模板中解决这个烦恼?

感谢您的帮助!

回答

0

请参阅these answers

+0

得到爱django模板...这是一个黑客攻击的解决方案,但哦。感谢您的回应。 – 2010-06-09 21:05:30

1

解决方法是使用number_devices中的密钥,并在for循环中检查它是否等于number_devices提供的密钥。

{% for key in number_devices.keys %} 
    {% for implementation in implementations %} 
     {% ifequal key implementation.id %} you got it {% endifequal %} 
    {% endfor %} 
{% endfor %} 

似乎有点难看,但应该工作。

相关问题