When I render a page using the Django template renderer, I can pass in a dictionary variable containing various values to manipulate them in the page using {{ myVar }}
.
Is there a way to access the same variable in Javascript (perhaps using the DOM, I don’t know how Django makes the variables accessible)? I want to be able to lookup details using an AJAX lookup based on the values contained in the variables passed in.
Advertisement
Answer
The {{variable}}
is substituted directly into the HTML. Do a view source; it isn’t a “variable” or anything like it. It’s just rendered text.
Having said that, you can put this kind of substitution into your JavaScript.
<script type="text/javascript"> var a = "{{someDjangoVariable}}"; </script>
This gives you “dynamic” javascript.