如何使用烧瓶显示图像

时间:2021-02-15 11:38:29

标签: python flask flask-sqlalchemy

models.py

我可以在这里存储数据

class AddNote(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(64), unique=True)
    description = db.Column(db.String(120), unique=True)
    img_name = db.Column(db.String(32))
    img = db.Column(db.LargeBinary)

routes.py

从数据库中检索数据

@app.route('/')
def index():
    notes = AddNote.query.all()
    return render_template('index.html', title='NoteBook', notes=notes)

HTML 模板

如何在img标签中显示图片

{% for note in notes %}
    
        <img src="" class="card-img-top">
        
{% endfor %}

1 个答案:

答案 0 :(得分:0)

好吧,如果数据库图像是静态存储的,那么应该做一个简单的静态请求

<img src="{{ url_for('static',filename='{{note.img_name}}') }}"></img>