循环的Python烧瓶

时间:2015-02-25 05:38:09

标签: python flask

我正在尝试动态创建一个由sqlite查询创建的dict填充的html表。每行都有它自己的批准和拒绝按钮,它会在点击时执行查询。单击其中一个按钮时,下次重新加载该页面时,将禁用这些按钮。我的问题是,当我单击其中一个批准或拒绝按钮时,即使该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)

1 个答案:

答案 0 :(得分:1)

试试这个并告诉我你是否有问题。

<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>

<强>更新 您的问题是执行更新查询的地方。

您正在通过&#39; id&#39;选择记录值。 (字符串)作为它的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)