4
你好,我使用燒瓶,Jinja2的,SQLAlchemy的使用Python開發小的應用程序,... 我節省在我的數據庫binare數據:編碼瓶/ Jinja2的二進制數據
file = request.files['file']
# store the recipe
recipe = Recipe(None, session['user_in'], request.form['title'], request.form['text'],request.form['tags'], file.read())
db.session.commit()
,我想展現在我的應用程序的條目:
@app.route('/recipe/<id>', methods=['GET', 'POST'])
def show_entry(id):
return render_template('show_entry.html', entry=db_session.query(Recipe).get(id))
而且在我的模板,我有:
<img src="data:image/png;base64,{{ entry.image }}"/>
但我有unicode錯誤
UnicodeDecodeError: 'ascii' codec can't decode byte 0x89 in position 0: ordinal not in range(128)
你能幫我解決這個問題嗎?
感謝我使用base64.encode和它的作品 – Cospel 2014-09-30 11:28:19
@ Cospel - 沒問題。哪一個是正確的語法 - 屬性引用或字典查找? – mhawke 2014-09-30 11:30:29
+1。但是'entry'似乎是一個OR映射對象,因此修改'entry.image'實際上會改變數據庫中的對象(如果要提交事務)。所以我可能會做'render_template('show_entry.html',img = entry.image.encode('base64'))'並在模板中使用'img'。 – 2014-09-30 11:31:19