【问题标题】:Python sqlite table updates using HTMLPython sqlite 表使用 HTML 更新
【发布时间】:2021-05-08 00:54:08
【问题描述】:

我在 html 中添加一个表格,显示 sqlite fetchall 的结果并使用烧瓶提供 html,但我需要一种方法让用户在前端和数据库中删除特定的记录行.有没有办法做到这一点?提前感谢您的帮助!

我的 HTML 表格:

   <table style="text-align: center; width: 100%;">
               <thead>
                  <td>Column 1</td>
                  <td>Column 2</td>
                  <td>Column 3</td>
                  <td>Update</td>
                  <td>Delete</td>
               </thead>
               
               {% for row in rows %}
                  <tr>
                    <td>{{row["row1"]}}</td>
                    <td> {{row["row2"]}}</td>
                    <td>{{row["row3"]}}</td>
                    <td><button type="button" id="remove">Update Row</button></td>
                    <td><button type="button" id="remove">Delete Row</button></td>  
                  </tr>
               {% endfor %}
            </table>

我的 Python 代码:

@app.route('/database', methods=['GET', 'POST'])
def database():
    connection = sqlite3.connect(r'')
    connection.row_factory = sqlite3.Row
    cursor = connection.cursor()
    datasearch = "select * from Items where ID= " + str(session['ID'])
    print(datasearch)
    cursor.execute(datasearch)
    rows = cursor.fetchall()
    return render_template("database.html", rows = rows)

【问题讨论】:

    标签: python html sqlite flask


    【解决方案1】:

    可以在button标签中使用onClick触发发送URL,类似

    <button onClick='window.location.href="/database/delete/{{row["row1"]}}"'>
    

    虽然那里有嵌套的引号。处理这些事情的常用方法是将行放在

    中,其中 字段包含您要发送的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-09
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多