7👍
✅
You should try this in view.py (do not forget to add the import json
):
array = [['X', 'Y', 'Z'], [1, 2, 3], [4, 5, 6]]
return render_to_response('page2.html', {'array': json.dumps(array)})
Then in the template use the raw value array without quotes:
var djangoData = {{ array|safe }};
var data = google.visualization.arrayToDataTable(djangoData);
EDIT: using a custom JSONEncoder to handle the Decimal
type, stolen from this question:
class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
return float(o)
return super(DecimalEncoder, self).default(o)
Then in the view:
array = [['X', 'Y', 'Z'], [Decimal('1'), Decimal('2'), Decimal('3')]]
return render_to_response('page2.html', {
'array': json.dumps(array, cls=DecimalEncoder),
})
Source:stackexchange.com