[英]Django - passing python data to javascript
I have a javascript code in my js.html: 我的js.html中有一个JavaScript代码:
{% load static %}
<script src="{% static "vis.js" %}"></script>
<div id="mynetwork"></div>
<script type="text/javascript">
var nodes = new vis.DataSet([
{'id': 1, 'label': 'node 1'},
{'id': 2, 'label': 'node 2'},
{'id': 3, 'label': 'node 3'},
{'id': 4, 'label': 'node 4'},
{'id': 5, 'label': 'node 5'}
]);
var edges = new vis.DataSet([
{'from': 1, 'to': 3},
{'from': 1, 'to': 2},
{'from': 2, 'to': 4},
{'from': 2, 'to': 5},
{'from': 3, 'to': 3}
]);
var container = document.getElementById('mynetwork');
var data = {
nodes: nodes,
edges: edges
};
var options = {};
var network = new vis.Network(container, data, options);
</script>
I have a python variable called nodesJS which takes this form when printed: 我有一个名为nodesJS的python变量,该变量在打印时采用以下形式:
[{'id': 1, 'label': 'node 1'},
{'id': 2, 'label': 'node 2'},
{'id': 3, 'label': 'node 3'},
{'id': 4, 'label': 'node 4'},
{'id': 5, 'label': 'node 5'} ]
which is the same form that this javascript would use. 这是此javascript将使用的相同形式。 In my views.py i'm just passing nodesJS variable in a context. 在我的views.py中,我只是在上下文中传递nodesJS变量。 Now i wanted to write my js code in this style: 现在我想以这种风格编写我的js代码:
var nodes = new vis.DataSet({{nodesJS}});
And the problem is that it doesn't work. 问题是它不起作用。 Is there any way for js to treat my python variable the i want it to? js有什么方法可以处理我想要的python变量?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.