title: Flask - How can I query a BLOB image data and display it on HTML / Jinja2?
body: <p>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 &quot;Blob&quot; to my Postgres database.</p>
  <p>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.</p>
  <p>These are my tables:</p>
  <pre><code>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)
  </code></pre>
  <p>This is the route that saves the POST and the IMAGE to my database and static folder directory:</p>
  <pre><code>@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)
  </code></pre>
  <p>Also, when I query the post to see if I can retrieve the data, using something like <code>posts = Post.query.all()</code> and use <code>print(posts.link.read())</code>. I get an error saying <code>AttributeError: 'list' object has no attribute 'link'</code></p>
  <pre><code>@app.route('/')
  @app.route('/home')
  def home():
      posts = Post.query.all()
      print(posts.link.read())
      return render_template('home.html', title='Home', posts=posts)
  </code></pre>