【发布时间】:2021-02-08 15:56:28
【问题描述】:
我正在尝试在来自 SQLite 数据库的模板上显示文本和图像。 数据库模型:
class NewProduct(db.Model):
id = db.Column(db.Integer, primary_key=True)
product_name = db.Column(db.String(100), nullable=False)
product_description = db.Column(db.String(200), nullable=False)
product_date = db.Column(db.DateTime, default=datetime.utcnow)
product_img = db.Column(db.BLOB())
def __repr__(self):
return '<NewProduct %r>' % self.id
在 .html 中显示来自 db 的信息的功能:
@app.route('/products')
def products():
products = NewProduct.query.order_by(NewProduct.product_date.desc()).all()
product_img = b64encode(products.product_img).decode('utf-8')
return render_template('products.html', products=products, image=product_img)
HTML:
<body>
{% if products|length > 0 %}
{% for el in products %}
<p>Name: {{ el.product_name }}</p>
<p>Description: {{ el.product_description }}</p>
<p>Image: </p> <img src="data:;base64,{{ image }}">
<p>Date: {{ el.product_date }}</p>
{% endfor %}
{% else %}
<p>There are have no products</p>
{% endif %}
所以,我收到一个错误:AttributeError: 'list' object has no attribute 'product_img' in line product_img = b64encode(products.product_img).decode('utf-8')
我这样做是为了answer。但我无论如何都会得到这个错误。我在做什么,我该如何解决?
【问题讨论】: