How to display image on html from SQLAlchemy LargBinary column

Viewed 263

I'm writing a gallery web app that saves photos and display them. I'm using SQLAlchemy for the database and store the image in a LargeBinary column. When I try to display every image I run into the problme that for displaying html image you need the url for the <img src= and I dont know how to reach the url of the BinaryData column in the database, is there a different way? (I dont want to save all images in a folder because i already have then stored in the database)

from flask import Flask, render_template, url_for, request, redirect, send_file
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from io import BytesIO

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)

class GalleryImage(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    pic = db.Column(db.LargeBinary, nullable=False)
    content = db.Column(db.String(200), nullable=False)
    name = db.Column(db.String(200), nullable=False)
    date_time = db.Column(db.DateTime, default=datetime.utcnow)

    def __repr__(self):
        return '<Gl %r>' % self.id

I transfred the elements to html script, I did not save the images in a folder because they are saved in the database

{% for pic in gallery %}
<div class="gallery">
    <img src=WHAT TO WRITE HERE!? alt="Img" width="100" height="100">
    <div class="desc">{{pic.name}}, <br> {{pic.content}}</div>
</div>
{% endfor %}
0 Answers
Related