【发布时间】:2016-12-06 04:40:03
【问题描述】:
我正在 django(1.9) Python 2.7.10 中渲染图像。效果很好。
views.py
canvas = FigureCanvas(fig)
response = django.http.HttpResponse(content_type='image/png')
canvas.print_png(response)
plt.close(fig)
return response
上面的代码总是从行首开始在浏览器中发布图像。我想将图像定位到浏览器中的新位置。我尝试在模板中传递响应变量。
views.py
canvas = FigureCanvas(fig)
response = django.http.HttpResponse(content_type='image/png')
canvas.print_png(response)
plt.close(fig)
context = {'response': response}
return render(request, 'index.html', context)
index.html
<div align="center" style="text-align:center;">{{ response }} </div>
这里的问题是我得到了错误:
'utf8' 编解码器无法解码位置 27 中的字节 0x89:无效的起始字节。你传入了 HttpResponse status_code=200, "image/png" (class 'django.http.response.HttpResponse')
【问题讨论】:
标签: django python-2.7 matplotlib django-templates django-views