Python 3.12.4
Django 5.1.3
When using chart.js in Django and placing the script in the HTML page I get these weird JS errors. I have included a screen shot below as well as the code below that. I need to point out that it all works perfectly. The chart loads and the data is correct from the view query, no errors other than what I see in VSC on the html page.
I have a query view that generates the data and includes it in a context (normal stuff here) and rendering to an html page with
<canvas id="myChart" class="chart" width="400" height="200"></canvas>
set up and the script at the bottom of the page inside the body. If I hover over each error I see things like:
Property assignment expected.javascript
â,â expected.javascript
Type annotations can only be used in TypeScript files.javascript
Declaration or statement expected.javascript
Whatâs interesting if I take out this line
data: {{ data|safe }},
and replace it with this line with a hard var in it
data: myChartData,
all the errors go away. At first I thought maybe it was something to do with JS linting etc. But seem to not be. I appreciate all help on this. Thank you.
Screenshot of code in VSC with errors.
Copy of script used
<script>
var config = {
type: 'bar',
data: {
datasets: [{
label: "Epson NOC Monthly Totals", // label is what is printed at top of chart in the center
data: {{ data|safe }},
backgroundColor: [
"rgba(255, 99, 132, 0.2)",
"rgba(54, 162, 235, 0.2)",
"rgba(255, 206, 86, 0.2)",
"rgba(75, 192, 192, 0.2)",
"rgba(153, 102, 255, 0.2)",
"rgba(255, 159, 64, 0.2)",
],
borderColor: [
"rgba(255, 99, 132, 1)",
"rgba(54, 162, 235, 1)",
"rgba(255, 206, 86, 1)",
"rgba(75, 192, 192, 1)",
"rgba(153, 102, 255, 1)",
"rgba(255, 159, 64, 1)",
],
borderWidth: 1,
}],
labels: {{ labels|safe }}
},
options: {
responsive: true
}
};
window.onload = function() {
var ctx = document.getElementById('myChart').getContext('2d');
window.myBar = new Chart(ctx, config);
};
</script>