I'm trying to build d3 graphs with backend data provided by Django Rest Framework. I want to write my d3 / js code in separate javacript file instead of cramming everything in HTML.
I can pass Django named URLs in HTML file but JS file doesn't accept this named URL. I'm planning to pass the whole data into a variable which contains the data in HTML and then reference it to JS file.
My code in HTML:
<script>
var dataToPlay
d3.json("{% url 'list-create-financial-data' %}", function (data) {
dataToPlay = data
});
console.log(dataToPlay)
</script>
But this var dataToPlay is not available outside the function. How can I make it available in separate JS file?