jaynp
jaynp

Reputation: 3325

Django `if` statement isn't working?

In my template, I added the following debugging statement:

<script>
  console.log("leaderboard? {{ client_settings.LEADERBOARD_ENABLED }}");
</script>

On the console, I see:

[14:09:20.026] "leaderboard? false"

Later in my code, I have the following code:

{% if client_settings.LEADERBOARD_ENABLED %}
<button data-theme='a' onClick="$('.leaderboard').slideDown();">Leaderboard</button>
{% endif %}

which I would think cause the Leaderboard button not to appear... but it does! Can anyone see why this is?

Upvotes: 0

Views: 61

Answers (1)

bonsaiviking
bonsaiviking

Reputation: 6005

The Python value for boolean false is stringified "False" with a capital F. Since your console statement has "false" with lowercase f, the value of client_settings.LEADERBOARD_ENABLED is probably the string "false", which would be interpreted as boolean True.

The Pythonic way to change this would be to use True and False when setting the LEADERBOARD_ENABLED variable, instead of the strings "true" and "false". If that is not feasible, you could change the template test to:

{% if client_settings.LEADERBOARD_ENABLED == "true" %}

Upvotes: 4

Related Questions