【问题标题】:Using Flask to send form data to a database使用 Flask 将表单数据发送到数据库
【发布时间】:2017-10-11 22:35:50
【问题描述】:

我创建了一个带有文本字段和按钮的简单网页。我希望我的应用程序在单击按钮时使用文本字段的内容更新数据库中的记录。看起来很简单,但我无法弄清楚我错过了什么。到目前为止,这是我的代码:

app.py 示例

@app.route('/update-audit/', methods=['POST'])
def update_audit(test_name, description):
    cur = connect_db()
    cur.execute('UPDATE audit SET description = ? WHERE test_name = ?', (description, test_name,))
    return render_template('clicked.html')

audit.html 示例

<form action="{{ url_for('update_audit') }}" method="post">
    <td>{{ row[2] }}</td>
    <td>
        <input type="text" id="desc" value="{{ row[3] }}" size="140">
        <input type="hidden" name="update_audit" value="{{ row[2] }}, desc"/>
        <input type="submit" class="btn btn-success" value="Update"/>
    </td>
</form>

clicked.html

<!DOCTYPE html>
{% extends "layout.html" %}
{% block content %}
<body>
{{ form.description }}<br />
</body>
{% endblock %}

表格样本

id | tool name | test name | description
========================================
1  | "tool1"   | "test1"   | "update me!"

不确定我是否遗漏了一个基本概念(我玩过flask_wtf,但一无所获),或者我离实现这一目标还差一两步。

【问题讨论】:

    标签: python flask flask-sqlalchemy flask-wtforms


    【解决方案1】:

    为文本输入设置名称属性,以便与提交的表单一起发送。

    <input name="description" type="text" id="desc" value="{{ row[3] }}" size="140">
    

    更新您的视图函数以从request 的 POST 字典属性中获取描述。 test_name 也需要更新为合适的值。

    @app.route('/update-audit/', methods=['POST'])
    def update_audit():
        description = request.form.get('description')
        test_name = request.form.get('update_audit')
        cur = connect_db()
        with cur:
            cur.execute(
                'UPDATE audit SET description = ? '
                'WHERE test_name = ?;', (description, test_name,))
    
        # commit changes to the database
        return render_template('clicked.html')
    

    【讨论】:

    • 感谢您的回答,虽然我得到了这个回溯:AttributeError: 'Request' object has no attribute 'POST'
    • 另外,我需要将测试名称传递给 update_audit,以便数据库知道我要更新哪一行。这应该发生在 audit.html 中,并且 row[2] 包含测试名称。
    • 应该是request.form 而不是request.POST
    • 将数据发送到 clicked.html 视图,但数据库仍未更新。不过关闭!
    • 是的,您需要将更改提交到数据库。
    【解决方案2】:

    想通了:

    app.py 示例

    @app.route('/update-audit/', methods=['POST'])
    def update_audit():
        description = request.form.get('description')
        test_name = request.form.get('test_name')
    
        sql = 'UPDATE audit SET description=? WHERE test_name=?'
        conn = sqlite3.connect(DATABASE)
        cur = conn.cursor()
        cur.execute(sql, (description, test_name))
        conn.commit()
        conn.close()
    
        return render_template('clicked.html', data=(test_name, description))
    

    audit.html 示例

    <form action="{{ url_for('update_audit') }}" method="POST">
        <td>
            <input type="hidden" name="test_name" value="{{ row[2] }}">{{ row[2] }}</input>
        </td>
        <td>
            <input type="text" name="description" id="desc" value="{{ row[3] }}" size="100" maxlength="140"/>
            <input type="submit" class="btn btn-success" value="Update"/>
        </td>
    </form>
    </tr>
    

    答案是正确的 SQL-Alchemy 命令的组合,并确保我通过 audit.html 中的两个输入标签将数据发送到 update_audit 函数。

    【讨论】:

      【解决方案3】:

      你的 render_template 应该有一个表单参数:

      return render_template('clicked.html', form=form)

      在您提供的代码中也不清楚在 python 中处理表单的位置以及变量行的来源。

      【讨论】:

      • row 是数据库中 iter 数据的当前索引。当我加载 audit.html 时,我传递了一个查询结果,该查询要求提供表示例中的所有数据。
      猜你喜欢
      • 2020-04-11
      • 2016-03-23
      • 1970-01-01
      • 2014-01-20
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 2019-04-30
      • 2021-08-21
      相关资源
      最近更新 更多