129
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
6
- [Django]-Multiple Database Config in Django 1.2
- [Django]-How to find out the request.session sessionid and use it as a variable in Django?
- [Django]-How to POST a django form with AJAX & jQuery
-1
solution for 2022:
<script>
const isAuthenticated = ${str(user.is_authenticated).lower()}
</script>
- [Django]-Are there any plans to officially support Django with IIS?
- [Django]-How to use Django ImageField, and why use it at all?
- [Django]-"no such table" exception
Source:stackexchange.com