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

Viewed 67

what excatly i am trying to do is to send csv data from views.py in my django app to a chart.js file i am using in the front this is chartjs file :

// 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
                    }
                }]
            }
        }
});

im trying to get the lists of number lf and ll but in my console its showing an error i couldn't figure it out : enter image description here

and here is my 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)
1 Answers

just try to inject the data in a javascript block from html it works so fine

Related