错误:函数返回 None 或以 return 语句结束.但没有找到在哪里



我正在尝试根据用户填写的表单提交的数据更新数据库数据,但收到此错误:

[类型错误:视图函数未返回有效的响应。该函数要么返回 None,要么在没有 return 语句的情况下结束。

错误堆栈跟踪:

Traceback (most recent call last):
      File "C:UsersthanaphonsaePycharmProjectsKudsonvenvlibsite-packagesflaskapp.py", line 2309, in __call__
        return self.wsgi_app(environ, start_response)
      File "C:UsersthanaphonsaePycharmProjectsKudsonvenvlibsite-packagesflaskapp.py", line 2295, in wsgi_app
        response = self.handle_exception(e)
      File "C:UsersthanaphonsaePycharmProjectsKudsonvenvlibsite-packagesflaskapp.py", line 1741, in handle_exception
        reraise(exc_type, exc_value, tb)
      File "C:UsersthanaphonsaePycharmProjectsKudsonvenvlibsite-packagesflask_compat.py", line 35, in reraise
        raise value
      File "C:UsersthanaphonsaePycharmProjectsKudsonvenvlibsite-packagesflaskapp.py", line 2292, in wsgi_app
        response = self.full_dispatch_request()
      File "C:UsersthanaphonsaePycharmProjectsKudsonvenvlibsite-packagesflaskapp.py", line 1816, in full_dispatch_request
        return self.finalize_request(rv)
      File "C:UsersthanaphonsaePycharmProjectsKudsonvenvlibsite-packagesflaskapp.py", line 1831, in finalize_request
        response = self.make_response(rv)
      File "C:UsersthanaphonsaePycharmProjectsKudsonvenvlibsite-packagesflaskapp.py", line 1957, in make_response
        'The view function did not return a valid response. The'
    TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.

这是我的 HTML 表单

<!DOCTYPE html>
<html lang="en">
<center>
<tbody>
<table id="myTable" class="table table-sm" cellspacing="0">
<form action ="{{ url_for('editid',id=rows[0])}}" method="POST">
    <h1>ร้านสาขา {{rows[2]}}</h1>
    ผลการแก้ไขข้อมูล: {{ msg }}
<thead class="thead-dark">
    <tr class="header active">
        <th scope="col"><input name="date" value="{{rows[1]}}"></th>
        <th scope="col">PMA</th>
        <th scope="col">UPSD</th>
        <th scope="col">BPSD</th>
    </tr>
</thead>
    <tbody>
    <tr>
        <th scope="row">เช้า</th>
        <td>62</td>
        <td><input name="mo_62_upsd" value="{{rows[3]}}"></td>
        <td><input name="mo_62_bpsd" value="{{rows[4]}}"></td>
    </tr>
    <tr></tr>
    <tr>
        <th scope="row">เช้า</th>
        <td>87</td>
        <td><input name="mo_87_upsd" value="{{rows[5]}}"></td>
        <td><input name="mo_87_bpsd" value="{{rows[6]}}"></td>
    </tr>
    <tr>
        <th scope="row">บ่าย</th>
        <td>62</td>
        <td><input name="af_62_upsd" value="{{rows[7]}}"></td>
        <td><input name="af_62_bpsd" value="{{rows[8]}}"></td>
    </tr>
    <tr>
        <th scope="row">บ่าย</th>
        <td>87</td>
        <td><input name="af_87_upsd" value="{{rows[9]}}"></td>
        <td><input name="af_87_bpsd" value="{{rows[10]}}"></td>
    </tr>
    <tr>
        <th scope="row">ดึก</th>
        <td>62</td>
        <td><input name="ev_62_upsd" value="{{rows[11]}}"></td>
        <td><input name="ev_62_bpsd" value="{{rows[12]}}"></td>
    </tr>
    <tr>
        <th scope="row">ดึก</th>
        <td>87</td>
        <td><input name="ev_87_upsd" value="{{rows[13]}}"></td>
        <td><input name="ev_87_bpsd" value="{{rows[14]}}"></td>
    </tr>
    <tr></tr>
<center>
    <td width="50%"><input type="submit" value="submit"/></td>
    <tr></tr>
    <td><a href = "/kudson/update">กลับหน้าหลัก</a></td>
</center>
    </tbody>
</form>
</table>
</center>
</html>

这是我的 Python 路由代码

@app.route('/kudson/edit/<id>', methods=['POST', 'GET'])
def editid(id):
        if request.method == 'POST':
            pass
        else:
            try:
                id = request.form['no']
                date = request.form['date']
                mo_62_upsd = request.form['mo_62_upsd']
                mo_62_bpsd = request.form['mo_62_bpsd']
                mo_87_upsd = request.form['mo_87_upsd']
                mo_87_bpsd = request.form['mo_87_bpsd']
                af_62_upsd = request.form['af_62_upsd']
                af_62_bpsd = request.form['af_62_bpsd']
                af_87_upsd = request.form['af_87_upsd']
                af_87_bpsd = request.form['af_87_bpsd']
                ev_62_upsd = request.form['ev_62_upsd']
                ev_62_bpsd = request.form['ev_62_bpsd']
                ev_87_upsd = request.form['ev_87_upsd']
                ev_87_bpsd = request.form['ev_87_bpsd']

            with sql.connect(con_string) as con:
                cur = con.cursor()
                params = date, mo_62_upsd, mo_62_bpsd, mo_87_upsd, mo_87_bpsd, af_62_upsd, af_62_bpsd, af_87_upsd, af_87_bpsd, ev_62_upsd, ev_62_bpsd, ev_87_upsd, ev_87_bpsd, id
                cur.execute("""UPDATE kudson_shift_daily_sell 
                                SET date=?, mo_62_upsd = %s, mo_62_bpsd = %s, mo_87_upsd = %s, mo_87_bpsd = %s, af_62_upsd = %s, af_62_bpsd = %s, af_87_upsd = %s, af_87_bpsd = %s, ev_62_upsd = %s, ev_62_bpsd = %s, ev_87_upsd = %s, ev_87_bpsd = %s 
                                WHERE id = %s""", params)
                con.commit()
                msg = "แก้ไขข้อมูลสำเร็จ"

            return render_template("kudson_edit.html", msg=msg)
        except Exception as e:
            return render_template("kudson_edit.html", msg=e)
if __name__ == '__main__':
    app.run(debug=True)

如果代码看起来不干净,我很抱歉,我是编码新手。

任何帮助将不胜感激!

问题是您的POST响应无效。正如@Shinra指出的那样,您只需在 POST 处理程序中执行pass,这不是有效的 HTTP 响应。发生这种情况是因为当您的表单执行时,它已被定向到您的editid路由:

<form action ="{{ url_for('editid',id=rows[0])}}" method="POST">

然后,您的"editid"路由设置为处理POSTGET路由:

@app.route('/kudson/edit/<id>', methods=['POST', 'GET'])

但您的POST路由不会返回有效的 HTTP 响应:

def editid(id):
        if request.method == 'POST':
            pass

您需要在提交此表单数据时决定要如何处理它。您可能希望验证它,将其保存在数据库中,并根据上述操作返回适当的重定向视图。

如果你正在做一个POST请求,问题是你根本不返回任何东西,你只有一个pass,代码继续运行,但没有其他事情要做或返回,函数退出而不返回接受的值。您必须返回有效的 HTTP 响应。

最新更新