我写了一些代码。
我可以在 BobProperty 中保存图像。
但是我无法将图像加载到 HTML 页面中...
源代码:
类产品(db.Model):
image = db.BlobProperty()
...
类添加:
productImage = self.request.get('image')
product.image = db.Blob(productImage)
product.put()
但我将 {{product.image}} 写入了 html 代码。但是有像��裤 ���� ���� ���� (������������ ��(:(�������� (������ �� (��>̢���� (������>������Y������K����
如果我想从数据存储中加载图像,我应该怎么做?
我使用辅助 View :
def serve_image(request, image):
if image == "None":
image = ""
response = HttpResponse(image)
response['Content-Type'] = "image/png"
response['Cache-Control'] = "max-age=7200"
return response
在模型中:
def get_image_path(self):
# This returns the url of serve_image, with the argument of image's pk.
# Something like /main/serve_image/1231234dfg22; this url will return a
# response image with the blob
return reverse("main.views.serve_image", args=[str(self.pk)])
只需使用 {{ model.get_image_path }}
即可。
(这是 django-nonrel,但我想你可以弄清楚它的作用)
还有一个帖子 here 对这个;你应该检查一下。
我是一名优秀的程序员,十分优秀!