我目前正在玩 pygooglechart 。 它是 Google 图表的 Python 包装器。
在我的views.py中,我有以下内容:
from pygooglechart import PieChart3D
def pytest(request):
chart = PieChart3D(250, 100)
chart.add_data([20, 10])
chart.set_pie_labels(['Hello', 'World'])
在我的 urls.py 中,我链接了 View :
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
(r'^report/$', 'App.djangoapp.views.reporting'),
(r'^facebook/', 'App.djangoapp.views.facebook'),
(r'^twitter/', 'App.djangoapp.views.twitter'),
(r'^pytest/', 'App.djangoapp.views.pytest'),
我知道我需要为我的 pytest View 添加 HttpResponse,但我不知道如何为访问该 url 的客户端呈现图像。当客户端访问该 url 时,它应该只生成图形的图像。我怎样才能做到这一点?
最佳答案
您可以使用几种方法。
重定向使用示例:
views.py
from django.http import HttpResponseRedirect from pygooglechart import PieChart3D def pytest(request): chart = PieChart3D(250, 100) chart.add_data([20, 10]) chart.set_pie_labels(['Hello', 'World']) return HttpResponseRedirect(chart.get_url())
模板使用示例:
views.py
from django.template import RequestContext from django.shortcuts import render_to_response from pygooglechart import PieChart3D def pytest(request): chart = PieChart3D(250, 100) chart.add_data([20, 10]) chart.set_pie_labels(['Hello', 'World']) context = RequestContext(request, { 'url_to_chart': chart.get_url() }) template = 'path/to/template.html' return render_to_response(template, context)
template.html
<img src="{{ url_to_chart }}" alt="" />
关于python - 如何在 Django View 中显示 pygooglechart?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17496575/