created on Dec. 5, 2012, 11:40 p.m. by Hevok & updated on Dec. 5, 2012, 11:44 p.m. by Hevok
Data can be passed from python to JavaScript by dumping the object into json and safe escaping it in the template (py2js_):
.. sourcecode:: python
# views.py ... from django.shortcuts import render from django.utils import simplejson ... def view(request, template='graph.html'): network = { 'data': { 'nodes': [ {'id': '1', 'label': 'Concepts', 'text':'Denigma Concepts'}, {'id': '2', 'label': 'Aspects', 'text':'Three aspects'} ], 'edges': [ {'id': '2to1', 'label': 'belongs_to', 'text':'A belonging to relationship', 'target':'1', 'source': '2'} ] } } network_json = simplejson.dumps(network) return render(request, template, {'network_json': network_json}) ...
.. sourcecode:: js
# graph.html
...
<script type="text/javascript">
...
data = {{ network_json|safe }};
...
...
.. _py2js: http://stackoverflow.com/questions/1445989/passing-python-data-to-javascript-via-django
Comment on This Data Unit