我有一个 django 项目设置。我正在使用 pivottable.js ( https://github.com/nicolaskruchten/pivottable )添加一个带有数据透视表的新页面
我的问题是我无法从 pivottable.js jQuery 函数中加载来自 views.py 上下文 {{data}} 的 csv 数据。
在我的views.py中:
def sysdev_pivottable(request):
context = {}
csvString = ""
with open("W:\\data.csv", 'rb') as csvfile:
reader= csv.reader(csvfile, delimiter=',', quotechar='|')
for row in reader:
csvString = csvString + ','.join(row)
context['data'] = csvString
template = loader.get_template('pivottable.html')
return HttpResponse(template.render(context, request))
在 pivottable.html 中:
...
<h1> Test load: {{ data }}</h1> #this displays data properly
<script type="text/javascript">
$(function()
{
var input1 = "1,2,3,4" #test data declared within the function()
$("#output").pivotUI(input1, {}); #this loads properly
var input2 = {{data}} ##this doesn't work, would break the rest of the <script>
});
</script>
<div id="output" style="margin: 10px;">
</div>
我不知道如何{{data}}
从$(function(){})
. 从外部$(function(){})
我可以正确加载{{data}}
。谢谢您的帮助