how to pass the dictionary to django template using javascript

Viewed 59

I am trying to pass the dictionary values to django template through JavaScript

Views.py

population_count = (
    {'name': 'Year 1800', 'data': '[107, 31, 635, 203, 2]'},
    {'name': 'Year 1900', 'data': '[133, 156, 947, 408, 6]'},
    {'name': 'Year 2012', 'data': '[1052, 954, 4250, 740, 38]'},
)
population_count_js = json.dumps(population_count)

jsonstring

return render(request, 'index.html', {'data': population_country_js},{'count': population_count_js})

But I am getting the error like " Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse () at (index):17"

  var b = '{{count|safe}}';

  console.log(JSON.parse(b))

index.html

<script>
    var a = '{{data|safe }}';

    console.log(JSON.parse(a))

    var b = '{{count|safe}}';

    console.log(JSON.parse(b))


    Highcharts.chart('container', {
        chart: {
            type: 'column'
        },
        title: {
            text: 'Historic World Population by Region'
        },
        xAxis: {
            categories: JSON.parse(a)
        },
        series: JSON.parse(b)
    });
</script>

I except the output in the chart

Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse () at (index):17

0 Answers
Related