Filipe Ferminiano
Filipe Ferminiano

Reputation: 8791

Parsing json.dumps to HighCharts in Django

I'm trying to create a pie chart with HighCharts with Django (Mac maverick).

This is my views.py

    def piechart(request):
        responses_pie =  AnswerRadio.objects.values("body").annotate(Count("id"))
        res = []
        for cat in responses_pie:
            res.append([
                cat["body"],    
                cat["id__count"],
                ])
        return render(request, 'chart_code_v2.html', {'responses_pie_json': json.dumps(res)})

reponses_pie_json has 2 keys, body - string values - and id__cout - integer values. But, in template, I'm not getting how to input response_pie_json in data of HighCharts. I'm trying to use

data: {{ responses_pie_json|safe }}

But It just show a blank screen. What should i do?

Upvotes: 1

Views: 1562

Answers (1)

ssbb
ssbb

Reputation: 2025

Just make separate view which send only JSON data. And then get this data with jQuery:

$.getJSON('/your/view/url', function(response) {
  // create chart here
});

Upvotes: 1

Related Questions