[Django]-Using matplotlib with Django Generic views

6👍

There are two ways to do this depending on what you want to do.

  1. If you just want an image to be shown without any HTML, then just use a normal function view rand return a HttpResponse or if you really, really really want to use class based views override the get method and return a HttpResponse.

  2. If you want to show an HTML page with the image embedded somewhere in it. Then you can do two things

    1. Create a separate view that will respond with just the image as you did in your example, and in your HTML page just add <img src="path to that view url" />.

    2. Or if you don’t want a separate view for the image then you have to create an image in the DetailView, save it to byte stream, base64 encode it and return it in your context as (for example) image. Then in your template you create an image tag with the base64 encoded data using <img src="data:image/png;base64,{{ image }}"/>.

2👍

There is a nice example here.

http://wiki.scipy.org/Cookbook/Matplotlib/Django

Leave a comment