This question already has an answer here:
这个问题在这里已有答案:
- Serve a dynamically generated image with Django 2 answers
使用Django 2答案提供动态生成的图像
Currently, I am getting the HTTPRssponse using:
目前,我正在使用HTTPRssponse:
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:
我不知道如何在我的HTML模板中显示它。我试过的是在我的上下文中传递它:
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?
我正在动态生成此图像,它不是静态的。如何显示我的HTTPResponse?
1 个解决方案
#1
0
def imageResponseView(...) .... return response
def imageResponseView(...)....返回响应
bind url to this view
将url绑定到此视图
show image using <img src = "//url_to_that_view" />
使用显示图像
#1
0
def imageResponseView(...) .... return response
def imageResponseView(...)....返回响应
bind url to this view
将url绑定到此视图
show image using <img src = "//url_to_that_view" />
使用显示图像