Chartjs-How to get value of defaultdic in JS (Django framework)?

0👍

You should not pass javascript by rendering the object in the template. That will render it like the str(…) of the object. It is possible that for some objects, that will produce text that is a valid JavaScript expression, but often, as is here the case, it will not.

You can make use of the |json_script:… template filter [Django-doc] to render data as a JSON object, and then load this in a JavaScript script, so:

{{ chart_data|json_script:"my_chart" }}

<script type="text/javascript">
var my_chart = JSON.parse(document.getElementById('my_chart').textContent)
const data = {
    labels: labels,
    datasets: [{
      label: 'My First dataset',
      backgroundColor: 'rgb(255, 99, 132)',
      borderColor: 'rgb(255, 99, 132)',
      data: Object.values(my_chart),
    }]
  };
</script>

Leave a comment