My django template boolean variable isn’t working as expected in javascript

For what I see your auth_status variable seems to be a string, not a boolean. A variable with a non-empty string on javascript will evaluate to true on an if clause.

Anyhow, something like

<script>
    var auth_status = {{ user.is_authenticated }};
</script>

will not work because that will generate this HTML:

<script>
    var auth_status = True;
</script>

As Python’s True boolean is uppercased.

This should do the translation from Python to Javascript:

<script>
    var auth_status = {{ user.is_authenticated|yesno:"true,false" }};
</script>

Check yesno docs here: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#yesno

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)