通过ID编号删除烧瓶SQLITE SQALCHEMY删除条目



我正在尝试与烧瓶一起尝试sqlite,我已经添加了条目并显示了全部。现在我陷入了删除功能。所以我想知道我会尝试寻求帮助。这是我的算法:

@app.route('/deleted', methods = ['GET', 'POST'])
def deleted():
    if request.method == 'POST':
        if not request.form['id']:
            flash('Please enter the right number!','error')
        else:
            student1 = Students(request.form['id'])
            x = Students.query.filter_by(id=student1)
            db.session.delete(x)
            db.session.commit()
            flash('Record was deleted successfully ')
            return redirect(url_for('show_all'))
    return render_template('deleted.html')

这是我的html:

<!DOCTYPE html>
<html>
   <body>
      <h3>S Kickout the student</h3>
      <hr/>
      {%- for category, message in get_flashed_messages(with_categories = true) %}
         <div class = "alert alert-danger">
            {{ message }}
         </div>
      {%- endfor %}
      <form action = "/deleted/{{id}}" method = "post">
         <label for = "id">Id Number</label><br>
         <input type = "text" name = "id" placeholder = "Student Id" /><br>
         <input type = "submit" value = "Delete" />
      </form>
   </body>
</html>

有人可以告诉我我在哪里做错了吗?我无法弄清楚。这很容易,但我不仅明白了为什么找不到一个。我的本地主机上的错误。任何帮助,将不胜感激。我一无所获。

您可以通过从模板中的操作中删除id来解决此问题:

<form action = "/deleted" method = "post">

由于该方法是帖子,并且表单存储id,因此您可以使用request.form['id']访问它。

最新更新