如何将 json 目录传递给 django 框架内的模板?
How to pass json directory to the template within the django framework?
我是D3和django的新手,希望结合起来做可视化。我下载了一个code example of D3,它需要json文件作为数据源。
然后我在 Django 中写了一个模板,比如:
`
.link {
stroke: #ccc;
}
.node text {
pointer-events: none;
font: 10px sans-serif;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 1500,
height = 2000
var color = d3.scale.category20();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var force = d3.layout.force()
.gravity(.05)
.distance(100)
.charge(-100)
.size([width, height]);
d3.json("{{file}}", function(error, json) {
//d3.json("all_0.0007_0.15.json", function(error, json) {
force
.nodes(json.nodes)
.links(json.links)
.start();
var link = svg.selectAll(".link")
.data(json.links)
.enter().append("line")
.style("stroke", function(d) { return color(d.color); })
.attr("class", "link");
var node = svg.selectAll(".node")
.data(json.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
//.on('dblclick', connectedNodes); //Added code
node.append("circle")
.attr("r", function(d) { return d.degree;})
.style("fill", function (d) {return color(d.group);})
node.append("text")
.attr("dx", 3) //It means the offset of label and circle
.attr("dy", ".35em")
.text(function(d) { return d.name })
.style("font-size",function(d) { return d.degree*2+'px' })
.style("stroke", "gray");
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
});
});
</script>`
我尝试将我计算机中的目录传递给模板中的{{file}}。但似乎浏览器无法找到 json 文件,即使模板与 json 文件位于同一文件夹中。
任何人都可以告诉我如何将本地计算机中 json 文件的目录传递给模板?提前致谢。
Django 不会以这种方式提供静态页面。
因为每个发送到 Django 服务器的请求(在您的情况下 http:localhost:8888
)都会通过 Django 的 URL 调度程序(https://docs.djangoproject.com/en/1.7/topics/http/urls/)路由,您需要设置 Django url 路由来处理你可能想要的每一个 url,否则 Django 只会 404 请求。
为了提供静态文件,您需要设置 Django 以提供静态内容 (https://docs.djangoproject.com/en/1.7/howto/static-files/)。
我是D3和django的新手,希望结合起来做可视化。我下载了一个code example of D3,它需要json文件作为数据源。 然后我在 Django 中写了一个模板,比如: `
.link {
stroke: #ccc;
}
.node text {
pointer-events: none;
font: 10px sans-serif;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 1500,
height = 2000
var color = d3.scale.category20();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var force = d3.layout.force()
.gravity(.05)
.distance(100)
.charge(-100)
.size([width, height]);
d3.json("{{file}}", function(error, json) {
//d3.json("all_0.0007_0.15.json", function(error, json) {
force
.nodes(json.nodes)
.links(json.links)
.start();
var link = svg.selectAll(".link")
.data(json.links)
.enter().append("line")
.style("stroke", function(d) { return color(d.color); })
.attr("class", "link");
var node = svg.selectAll(".node")
.data(json.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
//.on('dblclick', connectedNodes); //Added code
node.append("circle")
.attr("r", function(d) { return d.degree;})
.style("fill", function (d) {return color(d.group);})
node.append("text")
.attr("dx", 3) //It means the offset of label and circle
.attr("dy", ".35em")
.text(function(d) { return d.name })
.style("font-size",function(d) { return d.degree*2+'px' })
.style("stroke", "gray");
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
});
});
</script>`
我尝试将我计算机中的目录传递给模板中的{{file}}。但似乎浏览器无法找到 json 文件,即使模板与 json 文件位于同一文件夹中。 任何人都可以告诉我如何将本地计算机中 json 文件的目录传递给模板?提前致谢。
Django 不会以这种方式提供静态页面。
因为每个发送到 Django 服务器的请求(在您的情况下 http:localhost:8888
)都会通过 Django 的 URL 调度程序(https://docs.djangoproject.com/en/1.7/topics/http/urls/)路由,您需要设置 Django url 路由来处理你可能想要的每一个 url,否则 Django 只会 404 请求。
为了提供静态文件,您需要设置 Django 以提供静态内容 (https://docs.djangoproject.com/en/1.7/howto/static-files/)。