Parsing json.dumps to HighCharts in Django

188 views
Skip to first unread message

Filipe Ferminiano Rodrigues

unread,
Dec 23, 2013, 1:11:41 AM12/23/13
to django...@googlegroups.com
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?

Russell Keith-Magee

unread,
Dec 23, 2013, 7:51:59 PM12/23/13
to Django Users
If you're getting a completely blank screen, one of four things is happening:

 * You're not hitting the view you think you are. That means your URL definition isn't actually hitting the piechart view - it's matching something else. If you're still in development, try dropping a print statement into the top of the view, and make sure that the right view is being invoked.

 * The template file is empty. If chart_code_v2.html is blank, then nothing will be rendered.

 * You're not hitting the right template. Are you sure that you know *which* chart_code_v2.html is being rendered? Again, try putting some dummy text into your template and make sure that the file you're saving is the file that is being found by the call to render().

 * The page is rendering HTML that contains nothing visible. For example, if your template is *only* rendering a chart, but there's no data being provided, it's possible that the rendered chart is blank. Use the "view source" option of your browser to see if content is actually being rendered; then you can work out if the problem is rendering the right template, or if the problem is in the way you're driving your graphics library.

Once you've worked out which of these is happening, you should be able to sort out a fix fairly quickly.

Yours,
Russ Magee %-) 

Reply all
Reply to author
Forward
0 new messages