Serve a dynamically generated image with Django Serve a dynamically generated image with Django ajax ajax

Serve a dynamically generated image with Django


I assume you're using PIL (Python Imaging Library). You need to replace your last line with (for example, if you want to serve a PNG image):

response = HttpResponse(mimetype="image/png")img.save(response, "PNG")return response

See here for more information.


I'm relatively new to Django myself. I haven't been able to find anything in Django itself, but I have stumbled upon a project on Google Code that may be of some help to you:

django-dynamic-media-serve


I was looking for a solution of the same problem

And for me this simple approach worked fine:

from django.http import FileResponsedef dyn_view(request):    response = FileResponse(open("image.png","rb"))    return response