I am trying to create a chart Using Chartjs and Django, i have a problem when i am trying to pass the data from views.py to js code.
so,this is my code in views.py..
def home(request):
labels = ["A", "B", "C", "D"]
data = [1,2,3,4]
return render(request, 'home.html',{'labels': labels
,'data': data,})
and this is my part of code in home.html ..
<script>
var labels = {{labels}};
var data = {{data}};
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'line',
// The data for our dataset
data: {
labels: labels,
datasets: [{
label:"chartLabel",
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
data:data,
}]
},
// Configuration options go here
options: {}
});
</script>
put when i use these tow lines in js ..
var labels = ["A", "B", "C", "D"];
var data = [1,2,3,4];
instead of this tow my code works fine.
var labels = {{labels}};
var data = {{data}};
