如何将Django视图返回的数据存储在JavaScript变量中?

时间:2015-03-17 10:35:16

标签: javascript django

我通过调用视图获取数据,我想将该数据存储在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>

1 个答案:

答案 0 :(得分:0)

只需像访问任何其他Django变量一样访问它:

<script language="JavaScript">

var mydata = "{{data}}";
document.write("Data is: " + mydata);

</script>