Is there is any way to access javascript
variable in Django
template tags
?
Can i do something like this ,
<!DOCTYPE html>
<html>
<body>
<script>
var javascriptvar=0;
</script>
{% if javascriptvar==0 %}
do this
{% else %}
do this
{% endif %}
</body>
</html>
No, the Django template is compiled server side. It is then sent to the client where their browser executes the JavaScript. Nothing that is changed by the JavaScript executing on the client browser can have an affect on the template. It's too late at that point.
However the JavaScript could do something like make another request from the server for more information. Or you could just pre-compute the value on the server before you send it to the client. If you are more explicit about what you are trying to do we should be able to help.
You can of course use Django templates to set JavaScript variables.
<script>
var myVar = '{{ py_var }}';
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With