5

I use pyGal for making charts, but I can't insert them in django templates. It works great, if I try like this in views.py

 return HttpResponse(chart.render())

But when I make something like this in my template in doesn't work at all

 {{chart.render}}
vZ10
  • 2,468
  • 2
  • 23
  • 33

4 Answers4

6

The tutorial on pygal is accurate, however the issue you are probably seeing is from flask escaping html by default. In your template add:

{{ graph|safe  }}

or

{{ graph.render()|safe  }}

Depending what you pass to the template.

This solved the issue for me.

Andrea
  • 11,801
  • 17
  • 65
  • 72
jpyth
  • 76
  • 1
  • 2
4

This is what I do (and it works swimmingly!):

views.py:

def myview(request):
    # do whatever you have to do with your view
    # customize and prepare your chart
    # save SVG chart to server
    mychart.render_to_file('/path/to/graph.svg') 
    # end your view. For instance:
    context = {'message': 'Hello Pygal!'}
    return render(request, 'path/to/mytemplate.html', context)

mytemplate.html:

<embed type="image/svg+xml" src="/path/to/graph.svg" width="90"/>

That is all!

BringBackCommodore64
  • 4,910
  • 3
  • 27
  • 30
2

I could write you the answer, but I believe that this tutorial will provide a more detailed explanation.

Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
  • I've already read this tutorial several times, but it didn't help. Maybe me is the problem )) – vZ10 Jul 12 '13 at 07:22
1

multiple ways to do it

one way is

in temlate:

<div id="chart">
   <embed type="image/svg+xml" src= {{ chart|safe }} />
</div>

In django view :

return render(request, "temlate.html" , { 
    "chart": date_chart.render_data_uri()
})
Saurabh Chandra Patel
  • 12,712
  • 6
  • 88
  • 78