gpt4 book ai didi

django:为模板提供动态(reportlab)png

转载 作者:行者123 更新时间:2023-12-01 01:09:18 24 4
gpt4 key购买 nike

我已经在这个问题上度过了一天,但没有成功,因此将不胜感激。

我使用 reportlab 生成了一个图表,并使用这个 tutorial 成功地将它渲染到浏览器中:

        d = MyBarChartDrawing()

#extract the request params of interest.
#I suggest having a default for everything.
if 'height' in request:
d.height = int(request['height'])
if 'width' in request:
d.width = int(request['width'])

if 'numbers' in request:
strNumbers = request['numbers']
numbers = map(int, strNumbers.split(','))
d.chart.data = [numbers] #bar charts take a list-of-lists for data

if 'title' in request:
d.title.text = request['title']


#get a GIF (or PNG, JPG, or whatever)
binaryStuff = d.asString('png')

return HttpResponse(binaryStuff, 'image/png')

我的问题是我想在模板中渲染这个 png,像这样(不起作用):
return render(request, "subscription/monitorSizes.html", {'form':form,'message':'','graph':binaryStuff})

我多年来一直在解决这个问题。这无疑是一个新手问题!为了挽救我的理智,希望得到有关这方面的建议。非常感谢 :)

编辑:我发现了另一个 stackoverflow Q在同一个问题上,但并没有真正解决。

最佳答案

在views.py中

def show_image(request):
d = MyBarChartDrawing()
#extract the request params of interest.
#I suggest having a default for everything.
if 'height' in request:
d.height = int(request['height'])
if 'width' in request:
d.width = int(request['width'])

if 'numbers' in request:
strNumbers = request['numbers']
numbers = map(int, strNumbers.split(','))
d.chart.data = [numbers] #bar charts take a list-of-lists for data

if 'title' in request:
d.title.text = request['title']


#get a GIF (or PNG, JPG, or whatever)
binaryStuff = d.asString('png')

return HttpResponse(binaryStuff, 'image/png')

def somepage(request):
return render(request, "subscription/monitorSizes.html", {'form':form,'message':'','graph':reverse('show_image')})

在 urls.py 中
...
url(r'^show_image/$', 'your_view.show_image', name='show_image'),
url(r'^somepage/$', 'your_view.somepage', name='somepage'),
...

在订阅/monitorSizes.html
...
{% if graph != '' %} <img src="{{ graph }}" alt="some_text"> {% endif %}
...

关于django:为模板提供动态(reportlab)png,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16358091/

24 4 0
文章推荐: jquery - 使