djang的view文件中
import json def eg(request): data=[{'name':'通过', 'value':20}, {'name': 'jack', 'value': 14}] return render(request,"html文件",{"data":json.dumps(data), "test": "hello", "list": json.dumps(['jack', 'john'])
html的js,注意需要在双括号外加上引号
<script type="text/javascript">
//字符串直接输出 var test = "{{test}}" console.log(test)
//列表最好加上safe输出,要求不对字符进行转义 var list = "{{list|safe}}" console.log(list) console.log(list.length)
//如果想用循环,可以再用JSON.parse转为对象后,再用for循环 var data = '{{ data|safe }}' data = JSON.parse(data) console.log(data) for (x in data){ console.log(data[x]['name']) }
标签:console,log,list,django,test,json,传递数据,js,data From: https://www.cnblogs.com/regit/p/16888554.html