如何在 Google App Engine 中加载 Blobproperty 图像? [英] How to load Blobproperty image in Google App Engine?
问题描述
我写了一些代码.
我可以将图像保存在 BobProperty 中.
I could save image in BobProperty.
但我无法将图像加载到 HTML 页面中...
But I cannot load image into HTML page...
源代码:
类产品(db.Model):
class Product(db.Model):
image = db.BlobProperty()
...
类添加:
productImage = self.request.get('image')
product.image = db.Blob(productImage)
product.put()
但我将 {{product.image}} 写入 html 代码.但是有像 袀 ( (:( (( >̢ ( > Y K
but i wrote {{product.image}} into html code. But there were like ��袀 ���� ���� ���� (����������� ��(:(������� (������� (��>̢��� (�������>������Y������K��
如果我想从数据存储区加载图像,我应该怎么做?
What should i do if i want load image from datastore?
推荐答案
我使用辅助视图:
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,但我想你可以弄清楚它的作用)
(this is django-nonrel, but I guess you could figure out what it does)
此外, 这里有一篇关于此的帖子;你应该检查一下.
Also, there is a post here about this; you should check it out.
这篇关于如何在 Google App Engine 中加载 Blobproperty 图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!