问题
Currently, I am getting the HTTPRssponse using:
def imgResponse(valid_image):
try:
with open(valid_image, "rb") as f:
return HttpResponse(f.read(), content_type="image/jpeg")
except:
red = Image.new('RGBA', (1, 1), (255,0,0,0))
response = HttpResponse(content_type="image/jpeg")
red.save(response, "JPEG")
return response
I am not sure how to now display this in my HTML template. What I've tried is to pass it in my context:
context = {"other_context_part": other_context_part, "my_image": my_image}
Then I have the following in my template:
{{ my_image }}
I am generating this image dynamically and it is not static. How do I display my HTTPResponse?
回答1:
def imageResponseView(...) .... return response
bind url to this view
show image using <img src = "//url_to_that_view" />
来源:https://stackoverflow.com/questions/33727816/i-have-an-images-httpresponse-how-do-i-display-it-in-a-django-template