0

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?

Hannan
  • 1,171
  • 6
  • 20
  • 39
  • 1
    Please refrain from deleting and reposting a question if it has been closed as a duplicate. – knbk Jul 23 '17 at 21:55
  • Thanks but I rephrased it completely and tagged it under d3 and django instead under javascript. The question relates to D3 and Django and not ajax as marked by duplicate. – Hannan Jul 23 '17 at 22:01
  • Still no reason to delete and repost the question. People will come along and decide whether it should be reopened. – knbk Jul 23 '17 at 22:04

0 Answers0