我有一个django项目设置。我正在使用pivottable.js (https://github.com/nicolaskruchten/pivottable)添加一个带有数据透视表的新页面。
我的问题是,我无法从pivottable.js jQuery函数内的数据上下文{{ views.py }}加载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>我不知道如何从$(function(){})中正确加载{{data}}。从$(function(){})外部,我可以正确地加载{{data}}。谢谢你的帮助
发布于 2016-08-09 17:17:32
正如@AnnaVracheva提到的,我的主要问题是忘记了我的{{data}}周围的引号。此外,我使用jquery.csv (https://github.com/evanplaice/jquery-csv)对输入数据进行格式化,一切正常。
$(function() { var input = $.csv.toArrays("{{data}}") $("#output").pivotUI(input, { rows: ["RowValue"], cols: ["ColValue"] }); });
</script>
<div id="output" style="margin: 10px;">
</div>
https://stackoverflow.com/questions/38844457
复制相似问题