Flask SQLite SQalchemy通过ID号删除条目

时间:2017-09-24 07:32:25

标签: python sqlite flask

我和烧瓶一起尝试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>
谁能告诉我哪里做错了?我无法弄明白。这很容易,但我不知道为什么我会找到一个未找到的。我的localhost出错。任何帮助,将不胜感激。我无处可去。

1 个答案:

答案 0 :(得分:0)

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

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

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