0

我有一个 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}}。谢谢您的帮助

4

1 回答 1

0

正如@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>

于 2016-08-09T09:17:31.593 回答