So, I have a form where a user can posts a title, body and upload an image. I took that image from the form and saved it as a "Blob" to my Postgres database.
But I have a problem, I am confused on how to query for that image blob data and decode and show it to the user.
These are my tables:
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)
This is the route that saves the POST and the IMAGE to my database and static folder directory:
@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)
Also, when I query the post to see if I can retrieve the data, using something like posts = Post.query.all() and use print(posts.link.read()). I get an error saying 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)