制作一个 Flask 按钮来更新我的 sql 数据库文件

问题描述 投票:0回答:1

我正在尝试制作一个显示两个表的索引文件:

  1. 用户帐户详细信息(完成)
  2. 用户借用的设备。

在第二个表中,我尝试添加一个按钮,允许用户归还借用的设备(通过更新我的sql文件中的数据文件),但我确实卡在这里,不知道该怎么做。可以吗?

Python(app.py 文件)

@app.route("/", methods=["GET", "POST"])
@login_required
def index():
#Table 1 (Done)
    rows1 = db.execute("SELECT name AS n FROM users WHERE id = ?", session["user_id"])
    row1 = rows1[0]
    name = (row1["n"])
    rows2 = db.execute("SELECT user_id AS i FROM users WHERE id = ?", session["user_id"])
    row2 = rows2[0]
    user_id = (row2["i"])
    rows3 = db.execute("SELECT phone AS p FROM users WHERE id = ?", session["user_id"])
    row3 = rows3[0]
    phone = (row3["p"])

#Table 2
    borrowed = db.execute("SELECT equipments.eqp_name, timestamp, purpose, transactions.details, transactions.eqp_serial, return_date FROM transactions JOIN equipments ON equipments.eqp_serial = transactions.eqp_serial WHERE user_id = ? AND availability = 2", session["user_id"])

**    if request.method == "POST":
        # TODO**
    else:
        return render_template("index.html", name = name, user_id = user_id, phone = phone, borrowed = borrowed)

HTML(index.html 文件)

    </table>
    <br>
    <table>
        <tr style="background-color: #D6DBDF">
            <th>Items borrowed</th>
            <th>Serial</th>
            <th>Borrow Date</th>
            <th>Expected Return Date</th>
            <th>Purpose</th>
            <th>Details</th>
            <th>Return</th>

        </tr>
        <!--equipment borrowed under user for loop-->
        {% for borrow in borrowed %}
        <tr>
            <td>{{ borrow.eqp_name }}</td>
            <td>{{ borrow.eqp_serial }}</td>
            <td>{{ borrow.timestamp }}</td>
            <td>{{ borrow.return_date }}</td>
            <td>{{ borrow.purpose }}</td>
            <td>{{ borrow.details }}</td>
            <form method = "post">
            <td><button name="return">Return</button></td>
            </form>
        </tr>
        {% endfor %}
    </table>

我希望它看起来像这样。 (https://i.stack.imgur.com/zUMQ0.png) 一旦点击归还栏按钮,该设备的sql数据就会更新为可用,以便其他用户借用。介意帮我偷看吗?预先感谢!

python sql sqlite flask button
1个回答
0
投票

是的,可以在第二个表中添加一个按钮,允许用户通过更新 SQL 文件中的数据文件来归还借用的设备。

在index.html中

<tr>
    <td>{{ borrow.eqp_name }}</td>
    <td>{{ borrow.eqp_serial }}</td>
    <td>{{ borrow.timestamp }}</td>
    <td>{{ borrow.return_date }}</td>
    <td>{{ borrow.purpose }}</td>
    <td>{{ borrow.details }}</td>
    <td>
        <form method="post" action="/return_equipment">
            <input type="hidden" name="eqp_serial" value="{{ borrow.eqp_serial }}">
            <button type="submit" name="return">Return</button>
        </form>
    </td>
</tr>

现在更新app.py

from flask import request, redirect, url_for

@app.route("/return_equipment", methods=["POST"])
@login_required
def return_equipment():
    if request.method == "POST":
        eqp_serial = request.form.get("eqp_serial")

        # Update equipment availability to "available"
        db.execute("UPDATE equipments SET availability = 1 WHERE eqp_serial = ?", eqp_serial)
    # You may want to update the transaction or perform additional actions here.

return redirect(url_for("index"))

现在,当用户单击“返回”按钮时,会向 /return_equipment 端点发送带有设备序列号的 POST 请求。然后服务器将更新数据库中设备的可用性。

© www.soinside.com 2019 - 2024. All rights reserved.