【发布时间】:2018-08-31 13:38:54
【问题描述】:
我在电路模型中有一个属性,它为每个直径计算该直径对应的总管道长度。代码如下:
@property
def pipe_lengths(self):
components = Component.objects.filter(circuit=self)
diameters = Diameter.objects.filter(project=self.system.project, material = self.material_type)
pipe_dict = {}
length = 0
for diameter in diameters:
for component in components:
if component.diameter == diameter:
length += component.length
pipe_dict[diameter] = length
length = 0
print("PIPE_DICT IS: " + str(pipe_dict))
return pipe_dict
这似乎工作正常,打印语句的输出似乎没问题。但是现在我想在我的模板中访问这个字典,下面是我已经拥有的简化代码:
{% extends 'base.html' %}
{% block content %}
<h2>Circuit name: {{ circuit.circuit_name }}
<h3><strong>Piping lengths</strong></h3>
<table border="1" style="width:100%">
<tbody>
<tr>
<td><h3>nominal diameter</h3></td>
<td><h3>length [m]</h3></td>
</tr>
{% for diameter in circuit.pipe_lengths %}
<tr>
<td><h3>{{ diameter.nom_diameter }}</h3></td>
<td><h3>{{ circuit.pipe_lengths[diameter] }}</h3></td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
我可以访问
diameter.nom_diameter。
但是,当我尝试通过代码访问特定的长度值时:
circuit.pipe_lengths[diameter]
这不起作用。我收到以下错误:
TemplateSyntaxError at /solgeo/28/system/182/circuit/295/
Could not parse the remainder: '[diameter]' from 'circuit.pipe_lengths[diameter]'
非常感谢您的帮助。
【问题讨论】:
标签: python django dictionary templates