[英]Python Django: Pass json from view to javascript in template
I am new to django. 我是django的新手。
From my findings, I tried this way, but didn't work. 从我的发现中,我尝试了这种方式,但是没有成功。
<script type="text/javascript" >
var data = {{json}}
</script>
I am trying to use data table from this website, http://www.datatables.net/manual/data . 我正在尝试使用该网站http://www.datatables.net/manual/data中的数据表。
<script type="text/javascript" class="init">
var temp = '{{campaignList|escapejs}}'; // should be a list, but becomes a string
alert(typeof temp)
$(document).ready( function () {
$('#campaigns').DataTable({
data: temp,
columns: [
{ data: 'id' },
{ data: 'name' },
{ data: 'date' },
]
});
} );
</script>
When I check the type before passing into datatable, the type becomes string. 当我在传入数据表之前检查类型时,该类型变为字符串。 I also tried {{campaignList|escapejs}} without quote, but didn't work. 我也尝试了{{campaignList | escapejs}},但没有报价,但是没有用。
Any suggestion? 有什么建议吗? Thanks. 谢谢。
如果campaignList
是json字符串,请将其传递到safe
过滤器以防止转义:
var data = {{ campaignList|safe }};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.