我有一个图像的HTTPResponse。如何在Django模板中显示? [英] I have an image's HTTPResponse. How do I display it in a Django template?
本文介绍了我有一个图像的HTTPResponse。如何在Django模板中显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前,我正在使用HTTPRssponse:
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
我不知道如何在我的HTML模板中显示。我试过的是在我的上下文中传递:
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 }}
我是生成这个图像动态,它不是静态的。如何显示我的HTTPResponse?
I am generating this image dynamically and it is not static. How do I display my HTTPResponse?
推荐答案
def imageResponseView(...)
....
返回响应
def imageResponseView(...) .... return response
将URL绑定到此视图
使用< img显示图像src =// url_to_that_view/>
这篇关于我有一个图像的HTTPResponse。如何在Django模板中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文