【问题标题】:Flask SQLite SQalchemy Deleting Entry via ID NumberFlask SQLite SQlalchemy 通过 ID 号删除条目
【发布时间】:2017-09-24 07:32:24
【问题描述】:

我正在尝试 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>

谁能告诉我哪里做错了?我想不通。这很容易,但我不明白为什么我没有找到。我的本地主机上的错误。任何帮助,将不胜感激。我无处可去。

【问题讨论】:

    标签: python sqlite flask


    【解决方案1】:

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

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

    由于方法是POST,并且表单存储id,所以可以使用request.form['id']访问。

    【讨论】:

      猜你喜欢
      • 2017-07-26
      • 2015-10-22
      • 2018-07-28
      • 2015-01-25
      • 2013-10-23
      • 2011-06-22
      • 1970-01-01
      • 2020-08-17
      相关资源
      最近更新 更多