简体   繁体   English

在Django中显示来自数据库的图像?

[英]Display an image from the database in Django?

I have a read only db that I don't have the ability to change. 我有一个只读数据库,但我无法更改。 I know that it is extremely inefficient to store images in a database. 我知道将图像存储在数据库中效率极低。 However I don't have control over this one. 但是我对此没有控制权。 I need to display the image from the field in the database. 我需要显示数据库中字段的图像。 How can you do this in Django? 您如何在Django中做到这一点?

  1. Retrieve the image from the database as a binary, 从数据库中以二进制形式检索图像,
  2. write that directly into the http response content (with StringIO?), 将其直接写入http响应内容(使用StringIO?),
  3. set the relevant image mimetype/header 设置相关的图像模仿类型/标题

Don't forget to set the content length in the response. 不要忘记在响应中设置内容长度。 Experiement with 'streaming' if the images are huge. 如果图片很大,请使用“流”进行实验。

https://docs.djangoproject.com/en/1.5/ref/request-response/#httpresponse-objects https://docs.djangoproject.com/zh-CN/1.5/ref/request-response/#httpresponse-objects

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM