无论如何都知道使用烧瓶点击了哪张卡片 "add comment"

Is there anyway to know at which card "add comment" has clicked using flask

我正在尝试向我的 Web 应用程序添加评论部分,但是,我想知道它的“添加评论”点击了哪本书。我正在考虑获取该书的 ID。

这是我的 python

@app.route("/comment", methods=["GET", "POST"])
def comment():
    if request.method == "POST":
     if not request.form.get("comment"):
        return apology("must provide comment", 400)
     book = request.form.get("book_id")
     print(session["user_id"])
     print(book)
     id = session["user_id"]
     print(id)
     user = db.execute("SELECT username FROM users WHERE id=?", id)
     db.execute("INSERT INTO comment (comment, user,book_id) VALUES(?, ?,?)", comment, user,book)
     return render_template ("index.html")

    else:
        return render_template ("comment.html")

这是我的 html 代码

<form action="/comment" method="get">
<div class="container">
    <div class="row g-3">
        {% for book in books %}
        <div class="col-12 col-md-6 col-lg-4">
            <div class="card">
                <img src="{{book.volumeInfo.imageLinks.smallThumbnail}}">

               <div  class="card-body">
               <h5 style="color:red" class="card-title">{{book.volumeInfo.title}}</h5>
               <p style="color:blue" class="card-text">{{book.volumeInfo.authors}}</p>
               <p style="visibility:hidden" class="card-text">{{book.id}}</p>
               <input name="book" style="visibility:hidden" class="form-control mx-auto w-auto" name="book_id" value="{{book.id}}" type="text">
               <a href="{{book.volumeInfo.infoLink}}" class="btn btn-primary">More Info</a>
               <button value="{{book.id}}" class="btn btn-primary">add comment</button>
                </div>
            </div>
        </div>
        {% endfor %}
    </div>
</div>

这个HTML元素

<input name="book" style="visibility:hidden" class="form-control mx-auto w-auto" name="book_id" value="{{book.id}}" type="text">

属性 name 有两个不同的值,请尝试删除其中一个,即

<input name="book_id" style="visibility:hidden" class="form-control mx-auto w-auto" value="{{book.id}}" type="text">

然后写入是否可以在 Flask 应用程序中正确访问 book_id。另外,如果您需要 input 对用户不可见,您可以使用 type="hidden" 而不是修改样式,在这种情况下

<input name="book_id" value="{{book.id}}" type="hidden">