title: Python flask for loop
body: <p>I am trying to accomplish dynamically creating a html table populated by a a dict that is created from a sqlite query. each row has it's own approve and deny button, which will execute a query when clicked. When one of the buttons is clicked, the next time that page is reloaded, those buttons will be disabled. My problem is that when I click one of the approve or deny buttons, even though that onclick passes the id associated with that row so the query can update that row in the database, all of the rows in the database get updated on that one button click. Here is my code.</p>
  
  <pre><code>&lt;table border='1' align="center"&gt;
      {% for post in posts %}
  
          {% if post.approved == "True" or post.approved == "False" %}
                  &lt;tr&gt;
                      &lt;td&gt;
                          &lt;label&gt;{{post.id}}&lt;/label&gt;
                          &lt;h1 id ='grill1'&gt;{{post.photoName}}&lt;/h1&gt;
                          &lt;span&gt;
                              &lt;img id = 'photo1' src='{{post.photo}}' alt="Smiley face" height="200" width="200"&gt;
                          &lt;/span&gt;&lt;br&gt;
                          &lt;h5 id ='blurb1'&gt;
                              {{post.blurb}}
                          &lt;/h5&gt;
                          &lt;br&gt;
                          &lt;div style=" padding:10px; text-align:center;"&gt;
                              &lt;input type="button" id='approved{{post.id}}'  value="Approve" name="approve" onclick="window.location='/approve/{{post.id}}';"&gt; 
                              &lt;input type="button" id='denied{{post.id}}' value="Deny" onclick="window.location='/deny/{{post.id}}';"&gt;&lt;br&gt;
                          &lt;/div&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;script&gt;
                  document.getElementById("approved"+{{post.id}}).disabled = true;
                  document.getElementById("denied"+{{post.id}}).disabled = true;
                  &lt;/script&gt;
          {% else %}
                  &lt;tr&gt;
                      &lt;td&gt;
                          &lt;label&gt;{{post.id}}&lt;/label&gt;
                          &lt;h1 id ='grill1'&gt;{{post.photoName}}&lt;/h1&gt;
                          &lt;span&gt;
                              &lt;img id = 'photo1' src='{{post.photo}}' alt="Smiley face" height="200" width="200"&gt;
                          &lt;/span&gt;&lt;br&gt;
                          &lt;h5 id ='blurb1'&gt;
                              {{post.blurb}}
                          &lt;/h5&gt;
                          &lt;br&gt;
                          &lt;div style=" padding:10px; text-align:center;"&gt;
                              &lt;input type="button" id='approved{{post.id}}'  value="Approve" name="approve" onclick="window.location='/approve/{{post.id}}';"&gt; 
                              &lt;input type="button" id='denied{{post.id}}' value="Deny" onclick="window.location='/deny/{{post.id}}';"&gt;&lt;br&gt;
                          &lt;/div&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;script&gt;
                  document.getElementById("approved"+{{post.id}}).disabled = false;
                  document.getElementById("denied"+{{post.id}}).disabled = false;
                  &lt;/script&gt;
          {% endif %}
  
      &lt;!--&lt;tr&gt;
          &lt;td&gt;
              &lt;label&gt;{{post.id}}&lt;/label&gt;
              &lt;h1 id ='grill1'&gt;{{post.photoName}}&lt;/h1&gt;
              &lt;span&gt;
                  &lt;img id = 'photo1' src='{{post.photo}}' alt="Smiley face" height="200" width="200"&gt;
              &lt;/span&gt;&lt;br&gt;
              &lt;h5 id ='blurb1'&gt;
                  {{post.blurb}}
              &lt;/h5&gt;
              &lt;br&gt;
              &lt;div style=" padding:10px; text-align:center;"&gt;
                  &lt;input type="button" id='approved'  value="Approve" name="approve" onclick="window.location='/approve/{{post.id}}';"&gt; 
                  &lt;input type="button" id='denied' value="Deny" onclick="window.location='/deny/{{post.id}}';"&gt;&lt;br&gt;
              &lt;/div&gt;
          &lt;/td&gt;
      &lt;/tr&gt;--&gt;
      {% endfor %}
  &lt;/table&gt;
  </code></pre>
  
  <p>and corresponding function:</p>
  
  <pre><code>@app.route('/approve/&lt;int:post_id&gt;')
  def approve(post_id):
      con = connect_db('ugly_grills.db')
      con.execute('UPDATE grills SET approved = "True" WHERE ID = id;')
      con.commit()
      cur = con.execute('SELECT * FROM grills;')
      posts = [dict(fName=row[0], lName=row[1], email=row[2], phone=row[3], photoName=row[4],  photo=row[5], blurb=row[6], id=row[7], approved=row[8], vote=row[9]) for row in cur.fetchall()]
      con.close()
      return render_template("approval.html", posts=posts)
  </code></pre>