我通过调用视图获取数据,我想将该数据存储在JavaScript变量中。怎么做?
以下是我的观点:
def myView(request):
.
.
dataToReturn = "abc"
.
return render(request, 'a.html', {'data': dataToReturn})
说这是脚本:
<script>
var = myData; //I want to store returned data by django view in var myData
</script>
答案 0 :(得分:0)
只需像访问任何其他Django变量一样访问它:
<script language="JavaScript">
var mydata = "{{data}}";
document.write("Data is: " + mydata);
</script>