use*_*987 4 upload google-app-engine image
我写了一些代码.
我可以在BobProperty中保存图像.
但是我无法将图片加载到HTML页面中......
源代码:
class Product(db.Model):
image = db.BlobProperty()
...
Run Code Online (Sandbox Code Playgroud)
class add:
productImage = self.request.get('image')
product.image = db.Blob(productImage)
product.put()
Run Code Online (Sandbox Code Playgroud)
但我在html代码中写了{{product.image}}.但有像??? ???? ???? ???? (????????????????????????????????????????????????????????????????????????????? ?????ÿ??????ķ???
如果我想从数据存储中加载图像,该怎么办?
我使用辅助视图:
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
Run Code Online (Sandbox Code Playgroud)
在模型中:
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)])
Run Code Online (Sandbox Code Playgroud)
而只是使用{{ model.get_image_path }}
而不是.
(这是django-nonrel,但我想你可以弄明白它的作用)
此外,有一个帖子在这里关于这一点; 你应该检查一下.
归档时间: |
|
查看次数: |
1401 次 |
最近记录: |