[英]How to use Django list as a javascript variable in Django template?
I have a Django list by named list_a
: 我有一个名为
list_a
的Django列表:
[['Host Name', 'No. of Events'], [u'12.23.21.23', 0], [u'2.152.0.2', 2]]
I am using Google Chart API
in my Django template. 我在Django模板中使用
Google Chart API
。 I have to paas list_a
to the following java script variable: 我必须将
list_a
设置为以下Java脚本变量:
var data = google.visualization.arrayToDataTable([
['Host_name', 'No of events'],
['12.23.21.23', 0],
['2.152.0.2', 2]
]);
The above one is hard coded, but i want to use in the following way: 上面的代码是硬编码的,但是我想以以下方式使用:
var data = google.visualization.arrayToDataTable({{list_a}});
I tried with the above code and checked with the javascript console
option provided in chrome. 我尝试使用上述代码,并使用chrome中提供的
javascript console
选项进行了检查。 It show this: 它显示如下:
var data = google.visualization.arrayToDataTable([['Host Name', 'No. of Events'], [u'12.23.21.23', 0], [u'2.152.0.2', 2]]);
I compared with this above line with the hard coded code, It is very different because of following way: 我将此行与硬编码的代码进行了比较,由于以下方式的不同,这是非常不同的:
'
['12.23.21.23', 0] See the quotes on ip address field but not to the next field
How should i make the list_a
equivalent to the hard coded, So that i can easily get the required chart. 我应该如何使
list_a
等同于硬编码,以便我可以轻松获得所需的图表。
Apart from that why this '
is occurring when i tried to use it directly 除此之外,为什么这个
'
是当我试图直接使用它发生
I am using this https://google-developers.appspot.com/chart/interactive/docs/gallery/combochart sample of Google charts 我正在使用https://google-developers.appspot.com/chart/interactive/docs/gallery/combochart这个Google图表示例
Encode it as JSON, mark it safe, and print it out. 将其编码为JSON,将其标记为安全,然后将其打印出来。
var data = google.visualization.arrayToDataTable({{ list_a|json|safe }})
Unfortunately you'll need to write or find the json
filter, or encode it as JSON in the view. 不幸的是,您需要编写或找到
json
过滤器,或在视图中将其编码为JSON。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.