I have an image's HTTPResponse. How do I display it in a Django template? [duplicate]

旧街凉风 提交于 2019-12-13 04:48:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!