【发布时间】:2020-11-23 08:30:00
【问题描述】:
这个 python/flask 脚本创建了一个 svg QR 码字节流,但是当我尝试使用 Jinja2 模板 {{ qr[0] }} 渲染它时,它呈现为文本。如何将其渲染为图像?正如您从下面的代码中看到的那样,我已成功将数据传输到客户端 - 我不需要帮助 - 唯一的问题是数据格式错误 - svg 字节流 - 而我想显示该数据作为图像。
为了清楚起见,我必须在 render_template 中导出二维码,因为我还有其他变量对二维码有贡献,也需要导出。因此,<img src="{{ url_for('qr') }}"> 方法不起作用。
同样,我不希望数据在 URL 中可见。
import pyqrcode
from io import BytesIO
@app.route('/qr/<id>/<price>', methods=['GET', 'POST'])
def qr(id,price):
if id:
data = id+price
basestring = pyqrcode.create(data, error='H')
stream = BytesIO()
basestring.svg(stream, scale=5, module_color='#802929', background='#FFFFFF')
qr = stream.getvalue(), 200, {
'Content-Type': 'image/svg+xml',
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'}
return render_template('qr.html', qr=qr, data=data)
这是当前在 HTML 页面上呈现为文本的一部分:
b'\n
【问题讨论】:
-
这个问题类似于我要问的问题:stackoverflow.com/questions/33340728/…
标签: python html flask svg jinja2