kkk
kkk

Reputation: 97

How to compare two string on template in django?

I am trying to compare two string in template but always it is showing the result in else.Here I am adding my code where trans_his.trans_type= Debit but it always shows credit.

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

Upvotes: 0

Views: 3590

Answers (1)

Prakhar Trivedi
Prakhar Trivedi

Reputation: 8526

This is always failing because trans_his.trans_type is not an string but a unicode or an object that stringifies in Debit. If you really want to compare them, first convert this varaible trans_his.trans_type into string in your views by :

trans_his.trans_type = str(trans_his.trans_type)

And then compare it. Other wise use :

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

as Suggested by Anonymous.

Upvotes: 1

Related Questions