0

我是 django 和 python 的新手,我正在尝试使用 django 在 javascript 中使用 json 文件

当我不使用 django 时,Javascript 工作正常,但是当我使用 django 时,显示此错误:

"TypeError: node is null"

我这样称呼json:

d3.json("jsonfile.json", function(node) {
         ....
}

我试图将 json 与 html 文件和 js 文件一起放在模板目录中,但没有用

编辑1:

d3.json("jsonfile.json", function(error, node) {
         ....
}

向我展示:“TypeError:节点未定义”

所有的js都在同一个目录中:

<script src="{{ STATIC_URL }}js/d3.v3.min.js" type= text/javascript></script>
<script src="{{ STATIC_URL}}js/graph.js" type= "text/javascript"></script>

编辑2:

我的 JSON:

{  "coordinador":[    
    {"name":"ford","grupo":0},
    {"name":"user1","grupo":1},
    {"name":"user2","grupo":1},
    {"name":"user3","grupo":1},
    {"name":"car1","grupo":2},
    {"name":"car2","grupo":2},
    {"name":"car3","grupo":2},
    {"name":"car4","grupo":2},
    {"name":"car5","grupo":2}
  ],
  "links":[
    {"source":1,"target":0,"origen":"user1","objetivo":"ford"},
    {"source":2,"target":0,"origen":"user2","objetivo":"ford"},
    {"source":3,"target":0,"origen":"user3","objetivo":"ford"},
    {"source":4,"target":1,"origen":"car1","objetivo":"user1"},
    {"source":5,"target":1,"origen":"car2","objetivo":"user1"},
    {"source":6,"target":2,"origen":"car3","objetivo":"user2"},
    {"source":7,"target":2,"origen":"car4","objetivo":"user2"},
    {"source":8,"target":3,"origen":"car5","objetivo":"user3"}
  ]
}
4

1 回答 1

1

回答:

正如@Jason 所说,问题在于路径不正确。

d3.json("/static/js/jsonfile.json", function(node) {
         ....
}

感谢您的帮助!

于 2013-10-11T07:38:04.030 回答