簡體   English   中英

我如何將views.py中的數字列表發送到我在前面使用的chartjs文件?

[英]how can i send a list of numbers from views.py to a chartjs file im using for the front?

我正在嘗試做的是將 csv 數據從我的 django 應用程序中的 views.py 發送到我在前面使用的 chart.js 文件,這是 chartjs 文件:

 // comboBarLineChart var ctx_combo_bar = document.getElementById("comboBarLineChart").getContext('2d'); var comboBarLineChart = new Chart(ctx_combo_bar, { type: 'bar', data: { labels: ["a", "b", "c", "d", "e", "f", "j"], datasets: [{ type: 'bar', label: 'Followers', backgroundColor: '#FF6B8A', data: {{ lf }}, borderColor: 'white', borderWidth: 0 }, { type: 'bar', label: 'Likes', backgroundColor: '#059BFF', data: {{ ll }}, }], borderWidth: 1 }, options: { scales: { yAxes: [{ ticks: { beginAtZero:true } }] } } });

我試圖獲取編號 lf 和 ll 的列表,但在我的控制台中顯示錯誤我無法弄清楚:在此處輸入圖像描述

這是我的views.py function:

context={}
file_directory = 'media/fl.csv'
readfile(file_directory)
dashboard = []  
dashboard1 = []
for x in data['likes']:
    dashboard.append(x)
my_dashboard = dict(Counter(dashboard))  
print(my_dashboard)
values = my_dashboard.keys()
print(values)
listlikes = []
for x in values:
    listlikes.append(x)
print(listlikes)

for x in data['followers']:
    dashboard1.append(x)
my_dashboard1 = dict(Counter(dashboard1))  # {'A121': 282, 'A122': 232, 'A124': 154, 'A123': 332}
values1 = my_dashboard1.keys()
print(values1)
listfollowers = []
for x in values1:
    listfollowers.append(x)
print(listfollowers)

context = {
    'lf': listfollowers,
    'll': listlikes,
}
return  render(request,'static/assets/data/data_datatables.js',context)

只需嘗試從 html 將數據注入 javascript 塊中,它工作得很好

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM