【发布时间】:2016-04-03 09:00:06
【问题描述】:
我正在尝试从 Django(1.6.11) 模板中的 dict 呈现一个值。 unit_list 是单元(模型)的列表。 unit.unit_id 是单元的主键。 tags_dict 是标签的字典,其中 unit_ids 作为键,标签作为值。
{% for unit in unit_list %}
<tr>
<td>{{ unit.unit_id }}</td>
<td>{{ unit.version }}</td>
<td>{{ unit.release_dt|date:'Y-m-d' }} {{ unit.release_dt|time:'H:i:s' }}</td>
<td>{{ unit.update_dt|date:'Y-m-d' }} {{ unit.update_dt|time:'H:i:s' }}</td>
<td>
{{ tags_dict[unicode(unit)] }}
</td>
<td>{{ unit.last_modified|date:'Y-m-d' }} {{ unit.last_modified|time:'H:i:s' }}</td>
</tr>
{% endfor %}
但是我收到了这个错误:
Could not parse the remainder: '(unicode(unit))' from 'tags_dict.get(unicode(unit))'
【问题讨论】:
-
如果您删除对
unit的unicode调用,此错误会消失吗? -
然后错误是使用“(单位)”。我想我只需要在模板中删除带有 () 的方法调用
-
是的,
unicode是一个内置函数,但它在{{和}}中不起作用——这就是它失败的原因。
标签: python django django-templates jinja2