[Fixed]-Django: How to keep JavaScript code separate in static folder when it uses a python variable

2👍

As you mentioned in your own comment, you can save user_canvas_id in a js variable in home.html and access it in your js file. Something like this:

<head>
    <script>var user_canvas_id = "{{ user_canvas_id }}"</script>
    <script type="text/javascript" src="{% static 'canvas/canvasrender.js' %}"></script>
</head>
👤kaveh

-1👍

I am confused to as what is going on. I thought that the script tag inserts the js file in between the “<script> </script>

Kinda, but this is done client-side, by the browser who has no idea about the special meaning of curly braces in Django templates.

And static in the context of Django means just that: That Django serves that file as-is, without running it through the template engine. (Which wouldn’t help here anyway, as for the JavaScript file in isolation, the value of user_canvas_ids would be unknown, so the template engine couldn’t substitute anything useful for it.)

👤das-g

Leave a comment