title: How can I get the number of likes from my database to use in webpage?
body: <p>So I am making a website using flask. The database is a SQLite db and is made using flask-SQLAlchemy. I took some help from someone online and they helped me write the code for a functional like button. But, it is just an illusion, it does not function as one yet. It turns blue if someone clicks on it and it stays blue even if the page is refreshed. It turns black again if the user clicks on it while it was blue. So basically, I want the number of likes in the db to be incremented by 1 if a user liked the post and didn't remove their like. By extension, I want the number of likes to be decreased by 1 if the user removes their like. Also, I want to display the number of likes that page has. (including the like of the user, if they have liked the post)
  And the user must be logged in on my website to like posts.
  Here is the code for the route info:</p>
  <pre><code>@app.route('/read/&lt;string:post_name&gt;')
  def read(post_name):
      # page = request.args.get('page', 1, type=int)
      posts = Image.query.filter_by(post_name=post_name)
      pos = Image.query.filter_by(post_name=post_name).first()
      author = User.query.filter_by(id=pos.user_id).first()
  
      # posts = Image.query.filter_by(id=current_user.id).paginate(page=page, per_page=10)
      # print(posts.items)
  
      return render_template('readmanga.html', posts=posts, pos=pos, author=author)
  </code></pre>
  <p>Here is the code for the relevant table:</p>
  <pre><code>class Image(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      title=db.Column(db.String(120), nullable=False)
      date_posted = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
      img_location = db.Column(db.String(600), nullable=False)
      mimetype = db.Column(db.String(10))
      post_name = db.Column(db.String(150), nullable=False, unique=False)
      user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
      manga_name = db.Column(db.String(100), unique=False, nullable=False)
      likes = db.Column(db.Integer, nullable=False)
      reports = db.Column(db.Integer, nullable=False)
  </code></pre>
  <p>Here is the code for the template:</p>
  <pre><code>
  &lt;html&gt;
      &lt;head&gt;
      
          &lt;link rel=&quot;stylesheet&quot; href=&quot;url_for('static', filename='readmanga.css')&quot;&gt;
          &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css&quot;&gt;
  
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div id=&quot;container-1&quot; style=&quot;display: flex; justify-content: center; align-items: center; flex-direction: column;&quot;&gt;
              &lt;p style=&quot;font-family: sans-serif; text-decoration: underline; font-weight: bold; font-size: 32px;&quot;&gt;{{ pos.manga_name }}&lt;/p&gt;
              &lt;small style=&quot;font-family: sans-serif; font-weight: bold; margin-bottom: 15px;&quot;&gt;By: &lt;a href=&quot;{{ url_for('user', username=author.username) }}&quot;&gt;{{ author.username }}&lt;/a&gt;&lt;/small&gt;
          &lt;/div&gt;
          &lt;div class=&quot;container&quot; style=&quot;display: flex; flex-direction:column; justify-content:center; align-items:center;&quot;&gt;
              {% for post in posts %}
                  
                  &lt;img src=&quot;{{ url_for('static', filename='images/'+post.title) }}&quot; alt=&quot;&quot; class=&quot;items&quot; style=&quot;max-height: 1250px; max-width: 950px; object-fit: contain;&quot;&gt;
              
              {% endfor %}
          &lt;/div&gt;
          &lt;div class=&quot;wrap&quot;&gt;
              &lt;i class=&quot;fa fa-thumbs-up&quot; id=&quot;likebtn&quot; style=&quot;font-size: 64px;&quot;&gt;&lt;/i&gt;
              &lt;script&gt;
                  const likebtn = document.getElementById('likebtn');
  
                  
      
                  function setLikeBtnColor() {
                      likebtn.style.color = JSON.parse(localStorage.getItem('likebtn')) ? 'cornflowerblue':'black';
                  }
              
                  setLikeBtnColor();
                  
                  function myFunction() {
                      localStorage.setItem('likebtn', !JSON.parse(localStorage.getItem('likebtn')));
                      setLikeBtnColor();             
      
                      
                  }
                  likebtn.addEventListener('click', myFunction);
      
              &lt;/script&gt;
              &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  
  </code></pre>