0

Made image is not shown in html. I wrote in views.py

@login_required
def view_plot(request):
    left = np.array([1, 2, 3, 4, 5])
    height = np.array([100, 200, 300, 400, 500])
    plt.bar(left, height)
    filename = "output.png"
    save_fig=plt.savefig(filename)
    response = HttpResponse(content_type="image/png")
    save_fig.save(response, "PNG")
    return save_fig

in html

  <body>
    <img src='/accounts/view_plot' width=300 height=300>
  </body>

in urls.py

urlpatterns = [
    url(r'^view_plot$', views.view_plot,name='view_plot'),
]

But now ,web page is like enter image description here

cracked image is shown.

I do not know why this error happens. In Django app,output.png is saved. I doubt image is not made in view_plot.But i cannot know whether or not. What is wrong in my code?How should I fix this?

user8504021
  • 303
  • 5
  • 23
  • Not a direct solution but you might like to try showing the image as a base64 array so you don't have to clog a server with loads of image files... There an example of how to do this in [one of my questions](https://stackoverflow.com/q/31719138/1324033) – Sayse Oct 19 '17 at 12:37

1 Answers1

0

Make sure you have your STATIC_URL: '/static/' Set in settings.py. Then your static files like images should be in

-MyProject
--Static
---css
---js
---img
----all images

Then in your html template.

{% load static %}
<body>
<img src="{% static "my_app/example.jpg" %}" alt="My image"/>
</body> 

Check the Django documentation for static files info.

noes1s
  • 134
  • 7