2010-11-26 69 views
4

我寫了一些代碼。如何在Google App Engine中加載Blobproperty圖片?

我可以保存圖片在BobProperty。

但我不能圖像加載到HTML頁面...

源代碼:

類產品(db.Model):

 image = db.BlobProperty() 
      ... 

類添加:

productImage = self.request.get('image') 

    product.image = db.Blob(productImage) 

    product.put() 

但我將{{product.image}}寫入了html代碼。但有像 袀 ( (:( ( ( >̢ ( > Y K ׏

我應該怎麼做,如果我想從數據存儲加載圖像?

回答

5

我使用的輔助視圖:

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這個。你應該檢查一下。

相關問題