2013-02-02 92 views
2

我有一個圖像數據庫。我想使用jinja2模板在數據庫中顯示所有圖像。在GAE上使用jinja2顯示圖像

我送的數據庫對象如下:

class Default_tiles(db.Model): 
    name = db.StringProperty() 
    image = db.BlobProperty(default=None) 


class MainPage(webapp2.RequestHandler): 
    def get(self): 

    # get all the default tiles in the database 
    default_tiles_query = Default_tiles.all() 
    defaultTiles = default_tiles_query.fetch(10) 

    template_values = { 
     'defaultTiles': defaultTiles # contain all the defaut tiles to be displayed 
    } 

    template = jinja_environment.get_template('index.html') 
    self.response.out.write(template.render(template_values)) 

的Jinja2的模板在Default_tiles每個對象,以顯示每個圖像是:

<body> 
    {% for defaultTile in defaultTiles %} 
    {{ defaultTile.image }} 
    {% endfor %} 
</body> 

當這個運行出現以下錯誤日誌:

文件「/ Users/jamiefearon /桌面/開發/我的程序/ GAE功能齊全的網站與CSS,JavaScript和圖像/ index.html 」第24行,在頂層模板代碼 {{defaultTile.image}} UnicodeDecodeError錯誤:在位置0 '的ASCII' 編解碼器不能解碼字節0x89上:順序不在範圍內(128)

我懷疑我正在做一些錯誤的行{{ defaultTile.image }}

謝謝大家的幫助。

回答

4

您不能將二進制數據轉換爲html並顯示圖像,您需要使用IMG元素顯示圖像。

您需要使用處理程序提供圖像或使用inline base64 data顯示圖像。

+0

謝謝。你知道我能看到的任何例子來更好地理解如何做到這一點嗎?即如何在數據存儲中顯示數據庫中的所有圖像 –

+0

https://developers.google.com/appengine/articles/python/serving_dynamic_images –