Python 用于循环的烧瓶

Python flask for loop

我正在尝试动态创建一个 html table ,该 html table 由从 sqlite 查询创建的字典填充。每行都有自己的批准和拒绝按钮,单击时将执行查询。单击其中一个按钮后,下次重新加载该页面时,这些按钮将被禁用。我的问题是,当我单击批准或拒绝按钮之一时,即使 onclick 传递了与该行关联的 id,以便查询可以更新数据库中的该行,数据库中的所有行都会在该按钮上更新点击。这是我的代码。

<table border='1' align="center">
    {% for post in posts %}

        {% if post.approved == "True" or post.approved == "False" %}
                <tr>
                    <td>
                        <label>{{post.id}}</label>
                        <h1 id ='grill1'>{{post.photoName}}</h1>
                        <span>
                            <img id = 'photo1' src='{{post.photo}}' alt="Smiley face" height="200" width="200">
                        </span><br>
                        <h5 id ='blurb1'>
                            {{post.blurb}}
                        </h5>
                        <br>
                        <div style=" padding:10px; text-align:center;">
                            <input type="button" id='approved{{post.id}}'  value="Approve" name="approve" onclick="window.location='/approve/{{post.id}}';"> 
                            <input type="button" id='denied{{post.id}}' value="Deny" onclick="window.location='/deny/{{post.id}}';"><br>
                        </div>
                    </td>
                </tr>
                <script>
                document.getElementById("approved"+{{post.id}}).disabled = true;
                document.getElementById("denied"+{{post.id}}).disabled = true;
                </script>
        {% else %}
                <tr>
                    <td>
                        <label>{{post.id}}</label>
                        <h1 id ='grill1'>{{post.photoName}}</h1>
                        <span>
                            <img id = 'photo1' src='{{post.photo}}' alt="Smiley face" height="200" width="200">
                        </span><br>
                        <h5 id ='blurb1'>
                            {{post.blurb}}
                        </h5>
                        <br>
                        <div style=" padding:10px; text-align:center;">
                            <input type="button" id='approved{{post.id}}'  value="Approve" name="approve" onclick="window.location='/approve/{{post.id}}';"> 
                            <input type="button" id='denied{{post.id}}' value="Deny" onclick="window.location='/deny/{{post.id}}';"><br>
                        </div>
                    </td>
                </tr>
                <script>
                document.getElementById("approved"+{{post.id}}).disabled = false;
                document.getElementById("denied"+{{post.id}}).disabled = false;
                </script>
        {% endif %}

    <!--<tr>
        <td>
            <label>{{post.id}}</label>
            <h1 id ='grill1'>{{post.photoName}}</h1>
            <span>
                <img id = 'photo1' src='{{post.photo}}' alt="Smiley face" height="200" width="200">
            </span><br>
            <h5 id ='blurb1'>
                {{post.blurb}}
            </h5>
            <br>
            <div style=" padding:10px; text-align:center;">
                <input type="button" id='approved'  value="Approve" name="approve" onclick="window.location='/approve/{{post.id}}';"> 
                <input type="button" id='denied' value="Deny" onclick="window.location='/deny/{{post.id}}';"><br>
            </div>
        </td>
    </tr>-->
    {% endfor %}
</table>

及对应函数:

@app.route('/approve/<int:post_id>')
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)

试试这个,如果遇到问题请告诉我。

<table border='1' align="center">
    <tr>
        <th>Post ID</th>
        <th>Photo Name</th>
        <th>Photo</th>
        <th>Blurb!!!</th>
        <th>Actions</th>
    </tr>
{% for post in posts %}
    <tr>
        <td>{{ post.id }}</td>
        <td>
            <h1 class="grill1">{{ post.photoName }}</h1>
        </td>
        <td><img id="photo-{{post.id}}" src="{{ post.photo }}" alt="Smiley face" height="200" width="200"></td>
        <td>
            <h5 class="blurb1">{{ post.blurb }}</h5>
        </td>
        <td>
            <input type="button"{% if not post.approved %} disabled{% endif %} value="Approve" name="approve" onclick="window.location='/approve/{{post.id}}';"
            <input type="button"{% if not post.approved %} disabled{% endif %} value="Deny" name="deny" onclick="window.location='/deny/{{post.id}}';"
        </td>
    </tr>
{% endfor %}
</table>

更新: 你的问题是你在哪里执行更新查询。

您通过 'id'(字符串)选择记录值作为它的 ID,而不是通过 post_id 发送给函数的整数 ID。这就是您的更新应用于所有记录的原因。

改为这样做:

import sqlite3

@app.route('/approve/<int:post_id>')
def approve(post_id):
    con = sqlite3.connect('ugly_grills.db')
    con.row_factory = sqlite3.Row
    cur = con.cursor()
    cur.execute('UPDATE grills SET approved = "True" WHERE ID=?', (post_id,))
    # ------------------------------------------- DON'T FORGET THIS COMMA ^
    con.commit()
    cur.execute('SELECT * FROM grills')
    posts = cur.fetchall()
    return render_template("approval.html", posts=posts)