'Django / How to evaluate a django variable inside a bracket expression?
In my html file I need to define a variable for a js file.
<script>
var gltf_home = "{% static '/3d/ {{ scene.GltfFileToLoad }} ' %}";
</script>
which gives as an output :
/static/3d/%7B%7B%20scene.GltfFileToLoad%20%7D%7D
instead of
/static/3d/00-world.glb
And this alternative
var gltf_home = "{% static '/3d/' {{ scene.GltfFileToLoad }} %}";
gives
/static/3d/
What would be the correct way to do it ?
Solution 1:[1]
You can work with the |add
template filterĀ [Django-doc]:
var gltf_home = "{% static '/3d/'|add:scene.GltfFileToLoad %}";
But I would advise not to do this: perform the logic in the view, and work with the |json_script
template filterĀ [Django-doc], this will properly encode the data in a JSON blob, and thus prevents escaping, etc.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Willem Van Onsem |