好的,這是我的情況。我有一個我在django模板中迭代的通用對象數組。這些對象有許多子類,我想在模板中找出我正在處理的子類。這可能嗎?建議?Django模板對象類型
的代碼可能沿東西線(其中if語句包括一些虛構的語法):
<table>
<tr>
<th>name</th>
<th>home</th>
</tr>
{% for beer in fridge %}
<tr>
<td>
{{ beer.name }}
</td>
<td>
{% if beer is instance of domestic %}US of A{% endif %}
{% if beer is instance of import %}Somewhere else{% endif %}
</td>
</tr>
{% endfor %}
</table>
如何添加自定義過濾器:https://docs.djangoproject.com/en/stable/howto/custom-template-tags/ – clonejo 2017-01-08 00:59:24