如何在django中比较模板上的两个字符串?

如何在django中比较模板上的两个字符串?,django,django-templates,Django,Django Templates,我试图比较模板中的两个字符串,但它总是在else中显示结果。在这里,我添加了代码,其中trans_his.trans_type=Debit,但它总是显示credit {% if trans_his.trans_type == "Debit" %} <td>debit {{data.amount}}</td> {% else %} <td>credit {{data.amount}}</td> {% endif %}

我试图比较模板中的两个字符串,但它总是在else中显示结果。在这里,我添加了代码,其中trans_his.trans_type=Debit,但它总是显示credit

  {% if trans_his.trans_type == "Debit" %}
     <td>debit {{data.amount}}</td>
  {% else %}
     <td>credit {{data.amount}}</td>
  {% endif %}

这总是会失败,因为trans_his.trans_类型不是字符串,而是unicode或在借方中字符串化的对象。如果确实要比较它们,请首先通过以下方式将此变量trans_his.trans_类型转换为视图中的字符串:

trans_his.trans_type = str(trans_his.trans_type)
然后比较一下。其他明智的使用:

<td>{{ trans_his.trans_type }} {{ data.amount }}</td>

正如匿名者所建议的那样

你确定情况是一样的吗?你只需要做{{trans_his.trans_type}{{data.amount}}就可以了,这样看起来可以避免比较。对不起,这是我愚蠢的错误。我明白了,谢谢你的回复