python + flask 将数据从 html 传回 python 脚本(特别是标签的值)

python + flask passing data from html back to python script (specifically the value of a label)

我正在使用 python 和 flask 制作网络应用程序。我是新手,但已经完成了我想要完成的大部分工作。我被卡住的地方是我有一个标签,其值为 python 变量( {{id}} )此 ID 是我需要在 sqlite 数据库中更新的行的 ID。我的代码如下。当我点击批准按钮时,它会将我带到执行更新查询的路线,但我无法通过它传递 {{id}} 。如果我可以只使用 javascript 进行更新查询,这会容易得多,但我使用 javascript 找到的所有内容都适用于网络 sql,而不是 sql ite,尽管他们中的一些人说他们是为了 sqlite。

</script>
<table border='1' align="center">
    {% for post in posts %}
    <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"   value="Approve" name="approve" onclick="window.location='/approve;"> 

        <input type="button" value="Deny" onclick="window.location='/deny';"><br>
    </div>
        </td>
    </tr>
    {% endfor %}
</table>

为什么不直接做:

...
<input type="button"   value="Approve" name="approve" onclick="window.location='/approve/{{post.id}};"> 
<input type="button" value="Deny" onclick="window.location='/deny/{{post.id}}';">
...

然后你的 approve 和/或 deny 的 flask 路由可以只接受 post 的参数来批准或拒绝。即:

@app.route("/approve/<int:post_id>")
def approve(post_id):
    """approve this post!"""