因此,我有一个表单,用户可以在其中发布标题、正文并上传图像。我从表单中获取该图像并将其作为“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)
title = 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 和 IMAGE 保存到我的数据库和静态文件夹目录的路径:
@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))
# Regular Posts
form = PostForm()
if form.validate_on_submit():
post = Post(title=form.title.data,
body=form.body.data, link=image.read(), user_id=current_user.id)
db.session.add(post)
db.session.commit()
flash('Post submitted', 'success')
return redirect(url_for('home'))
return render_template('create_post.html', title='Create Post', form=form)
此外,当我查询帖子以查看是否可以检索数据时,使用类似 posts = Post.query.all()
并使用 print(posts.link.read ())
。我收到一条错误消息 AttributeError: 'list' object has no attribute 'link'
@app.route('/')
@app.route('/home')
def home():
posts = Post.query.all()
print(posts.link.read())
return render_template('home.html', title='Home', posts=posts)
最佳答案
我认为您应该将图像作为路径名存储到您的帖子表中,而不是 blob。
在数据库中将其更改为:
link = db.Column(db.String(30))
然后在添加post到数据库时将图像文件名路径的字符串传递到Post中
使用随机字符串重命名文件也是一个好习惯,因为许多用户可能会上传 myCatPicture.jpg
这会导致它覆盖该文件。
像这样就可以了
def get_random_string(length):
# Random string with the combination of lower and upper case
letters = string.ascii_letters
return ''.join(random.choice(letters) for i in range(length))
然后在保存图像时保存新文件名
if not allowed_images(image.filename):
flash('Invalid image extension!', 'danger')
return redirect(request.url)
else:
ext = os.path.splitext(file_name)[1] # get the file extension
new_filename = get_random_string(20) # create a random string
image.save(os.path.join(app.config['IMAGE_UPLOADS'], new_filename+ext)) # save with the new path
并在后期创建中使用新字符串。
post = Post(title=form.title.data,
body=form.body.data, link=os.path.join(app.config['IMAGE_UPLOADS'], new_filename+ext) , user_id=current_user.id) #here we are substituting the old blod with the new stored image path string
注意:
只要数据库中存储了路径,文件名并不重要。您始终可以查找它来获取图像/路径。
...
然后在您的模板中(因为您已经拥有所有帖子),您可以启动 for 循环
{% for post in range(len(posts)) %}
<h1> {{ post.title }} </h1>
<img src= {{post.link }} >
<p> {{ post.body }} </p>
{% endfor %}
这应该迭代每个帖子标题、图像和内容等。
我对此再次有点模糊。但我认为这几乎涵盖了它。
关于python - Flask - 如何查询 BLOB 图像数据并将其显示在 HTML/Jinja2 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63159952/