Python 如何将数据(id)放在表单(wtforms)中并在提交时重新获取

Python 如何将数据(id)放在表单(wtforms)中并在提交时重新获取,python,flask,wtforms,Python,Flask,Wtforms,我不熟悉wtforms,因此我非常感谢您的帮助 我需要在每个“删除”按钮附近显示书籍名称和名称。我读过,但不知道如何解决我的问题 所以我决定用另一种方式来做——我最好的办法是用id和名称以及提交返回id向模板呈现dict,但我还是做不到。下面是代码示例 是view.py @application.route('/delete', methods=['GET', 'POST']) def delete(): books_lib = mydatabase.get_all_books()

我不熟悉wtforms,因此我非常感谢您的帮助

我需要在每个“删除”按钮附近显示书籍名称和名称。我读过,但不知道如何解决我的问题

所以我决定用另一种方式来做——我最好的办法是用id和名称以及提交返回id向模板呈现dict,但我还是做不到。下面是代码示例

是view.py

@application.route('/delete', methods=['GET', 'POST'])
def delete():
    books_lib = mydatabase.get_all_books()
    form = DeleteForm(request.form)
    if request.method == 'POST':
        delete_id = form.id.data
        mydatabase.del_book(delete_id)
        return render_template('delete.html', form = form, books_lib = books_lib)
    return render_template('delete.html', form = form, books_lib = books_lib)
它是模板

<!DOCTYPE html>
<html>
<body>
    <h2>book to delete</h2>
<form action="/delete" name="delete" method="POST">
    {% if books_lib %}
        {% for id, book in books_lib.items() %}
            <p>{{book}}:<input type="submit" value="Delete" id="{{id}}">
        {% endfor%}
    {% endif %}
</form>
</body>
</html>
我是这样做的

模板

<p><a href="{{ url_for('delete_post', url=post.url) }}">delete</a></p>
现在,您可以在模板中生成post.id列表

    {% for post in posts %}
        {{post.id})
    {% endfor %}
要在列表中的每个项目旁边添加删除链接,请使用url\u

{% for post in posts %}    
<p>{{post.id}) <a href="{{ url_for('delete_post', id=post.id) }}">delete</a></p>
{% endfor %}
{%for posts in posts%}
{{post.id})

{%endfor%}
在这种情况下,我使用post id而不是post.url来标识我要删除的特定帖子,因此不要忘记在delete函数中修改@app.route

@app.route('/index')
def index():
    posts = Post.query.order_by(Post.pub_date.desc())  #SQLAlchemy query.
    return render_template('index.html', posts=posts) #sends all posts to template.
@app.route('/<int:id>/delete')
@app.route(“//删除”)

注意这个解决方案不需要一个表单。如果你想用复选框做批量删除,你只需要一个表单。

你真的需要它来删除一个表单吗?你会考虑在每本书旁边都有一个删除链接吗?我不知道如何使用它来获得图书ID。什么?
{% for post in posts %}    
<p>{{post.id}) <a href="{{ url_for('delete_post', id=post.id) }}">delete</a></p>
{% endfor %}
@app.route('/<int:id>/delete')