如何在django模板中嵌入pygal图表
我使用pyGal制作图表,但无法将它们插入django模板中。 如果我在views.py中这样尝试,效果会很好如何在django模板中嵌入pygal图表,django,django-templates,pygal,Django,Django Templates,Pygal,我使用pyGal制作图表,但无法将它们插入django模板中。 如果我在views.py中这样尝试,效果会很好 return HttpResponse(chart.render()) 但当我在中的模板中制作类似的内容时,根本不起作用 {{chart.render}} 我可以给你写答案,但我相信该教程将提供更详细的解释。关于pygal的教程是准确的,但是你可能看到的问题是默认情况下来自html。在模板中添加: {{ graph|safe }} 或 取决于传递给模板的内容 这为我解决了这
return HttpResponse(chart.render())
但当我在中的模板中制作类似的内容时,根本不起作用
{{chart.render}}
我可以给你写答案,但我相信该教程将提供更详细的解释。关于pygal的教程是准确的,但是你可能看到的问题是默认情况下来自html。在模板中添加:
{{ graph|safe }}
或
取决于传递给模板的内容
这为我解决了这个问题。这就是我所做的(它工作得很顺利!):
视图.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)
<embed type="image/svg+xml" src="/path/to/graph.svg" width="90"/>
mytemplate.html:
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)
<embed type="image/svg+xml" src="/path/to/graph.svg" width="90"/>
就这些 有多种方法可以做到这一点 一种方法是 在temlate:
<div id="chart">
<embed type="image/svg+xml" src= {{ chart|safe }} />
</div>
这个教程我已经读了好几遍了,但都没用。也许问题出在我身上