所以,我有一个表单,用户可以在其中发布标题、正文和上传图片。我从表单中获取了该图像并将其作为“Blob”保存到我的 Postgres 数据库中。
但是我有一个问题,我对如何查询该图像 blob 数据并解码并将其显示给用户感到困惑。
这些是我的表:
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(120), unique=True, nullable=False)
username = db.Column(db.String(30), unique=True, nullable=False)
password = db.Column(db.String(120), nullable=False)
date_joined = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
posts = db.relationship('Post', backref='author', lazy=True, passive_deletes=True)
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
titl = db.Column(db.String(100), nullable=False)
body = db.Column(db.Text, nullable=False)
link = db.Column(db.LargeBinary)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
user_id = db.Column(db.Integer, db.ForeignKey( 'user.id', ondelete='CASCADE'), nullable=False)
这是将 POST 和图像保存到我的数据库和静态文件夹目录的路径:
@app.route('/create/post', methods=['GET', 'POST'])
def create_post():
# Image upload and validation
if request.method == 'POST':
if request.files:
if allowed_image_size(request.cookies.get('filesize')):
flash('Image exceeds the maximum size limit of 5 MB!', 'danger')
return redirect(request.url)
image = request.files['uploadImg']
if image.filename == '':
flash('No image detected or file name is empty!', 'danger')
return redirect(request.url)
if not allowed_images(image.filename):
flash('Invalid image extension!', 'danger')
return redirect(request.url)
else:
filename = secure_filename(image.filename)
image.save(os.path.join(app.config['IMAGE_UPLOADS'], image.filename))
此外,当我查询帖子以查看是否可以检索数据时,使用类似的东西posts = Post.query.all()并使用print(posts.link.read()). 我收到一条错误消息AttributeError: 'list' object has no attribute 'link'
慕雪6442864
相关分类