【发布时间】:2020-05-07 08:49:52
【问题描述】:
我试过这样做,但图像没有出现在 HTML 页面上 这是一个烧瓶应用程序访问“图像”数据库,其中存储了具有唯一 ID 的图像 如何使用flask获取存储在数据库中的图像并在我正在渲染的html页面上显示图像
from flask_sqlalchemy import SQLAlchemy
from flask import *
from sqlalchemy import *
from sqlalchemy.orm import scoped_session, sessionmaker
from io import BytesIO
import base64
import image
#from sqlalchemy_imageattach.entity import Image, image_attachment
app = Flask(__name__)
app.secret_key = "manoj"
db = SQLAlchemy(app)
app.config["SQLALCHEMY_DATABASE_URI"] = 'mysql://root@localhost/image'
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)
class Images(db.Model):
__tablename__ = "imgs"
id = db.Column(db.Integer , primary_key = True , nullable=False)
img = db.Column(db.LargeBinary)
@app.route('/')
def index():
return render_template("parallax.html")
@app.route('/upload' , methods = ['POST'])
def upload():
img = request.files['img_file']
newfile = Images(img = img.read())
db.session.add(newfile)
db.session.commit()
return "File Uploaded : " + img.filename
@app.route('/download')
def download():
img = Images.query.filter_by(id = 1).first()
newimg = BytesIO(img.img)
img = image(newimg)
return render_template("image.html" , data = img)
if __name__ == "__main__":
app.run(debug = True)
【问题讨论】:
标签: python flask sqlalchemy