python 2.7 - Django Unicode Error While Rendering Image through template -


i rendering image in django(1.9) python 2.7.10. works fine.

views.py

canvas = figurecanvas(fig) response = django.http.httpresponse(content_type='image/png') canvas.print_png(response) plt.close(fig) return response 

the code above publishes image in browser beginning of line. want position image new location in browser. tried passing response variable in template.

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> 

the problem here error:

'utf8' codec can't decode byte 0x89 in position 27: invalid start byte. passed in httpresponse status_code=200, "image/png" (class 'django.http.response.httpresponse')

you need set own url view (note can view had listed works), add in urls.py:

  url('^show_graph/$', yourview, name="show_graph"), 

then can show image in html putting index.html:

<img src={% url "show_graph" %} height="400" width="400"> 

Comments

  1. I am using same method what you given above. But still i am facing same error.

    ReplyDelete

Post a Comment

Popular posts from this blog

Android volley - avoid multiple requests of the same kind to the server? -

magento2 - Magento 2 admin grid add filter to collection -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -