User007
User007

Reputation: 1559

Django template string comparison failed

{{ is_true }}

{% if is_true == "True" %}
    <h2>Say True</h2>
{% else %}
    <h2> False </h2>
{% endif %}

But instead it went to else clause even though {{ is_true }} returns True

Any idea?

def some_func():
   if ....:
     return True
   else:
     return False

Upvotes: 0

Views: 3379

Answers (1)

C&#233;sar
C&#233;sar

Reputation: 10119

You don't need to use "True" in your template:

{% if is_true == True %}

Or just:

{% if is_true %}

If you use "True" in your template then you are comparing the boolean True with the string "True" (which are not the same) and end up in the else clause of your template. In other words you would be doing:

{% if True == "True" %}
    <h2>Say True</h2>
{% else %}                     # You will end up here
    <h2> False </h2>
{% endif %}

You can find more info about Django's Template Language in the documentation

Upvotes: 4

Related Questions