错误:函数返回 None 或在没有返回语句的情况下结束。但在哪里找不到? [复制]

Posted

技术标签:

【中文标题】错误:函数返回 None 或在没有返回语句的情况下结束。但在哪里找不到? [复制]【英文标题】:Error: The function either returned None or ended without a return statement. but not found where? [duplicate] 【发布时间】:2019-02-01 09:55:23 【问题描述】:

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

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

错误堆栈跟踪:

Traceback (most recent call last):
      File "C:\Users\thanaphonsae\PycharmProjects\Kudson\venv\lib\site-packages\flask\app.py", line 2309, in __call__
        return self.wsgi_app(environ, start_response)
      File "C:\Users\thanaphonsae\PycharmProjects\Kudson\venv\lib\site-packages\flask\app.py", line 2295, in wsgi_app
        response = self.handle_exception(e)
      File "C:\Users\thanaphonsae\PycharmProjects\Kudson\venv\lib\site-packages\flask\app.py", line 1741, in handle_exception
        reraise(exc_type, exc_value, tb)
      File "C:\Users\thanaphonsae\PycharmProjects\Kudson\venv\lib\site-packages\flask\_compat.py", line 35, in reraise
        raise value
      File "C:\Users\thanaphonsae\PycharmProjects\Kudson\venv\lib\site-packages\flask\app.py", line 2292, in wsgi_app
        response = self.full_dispatch_request()
      File "C:\Users\thanaphonsae\PycharmProjects\Kudson\venv\lib\site-packages\flask\app.py", line 1816, in full_dispatch_request
        return self.finalize_request(rv)
      File "C:\Users\thanaphonsae\PycharmProjects\Kudson\venv\lib\site-packages\flask\app.py", line 1831, in finalize_request
        response = self.make_response(rv)
      File "C:\Users\thanaphonsae\PycharmProjects\Kudson\venv\lib\site-packages\flask\app.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 ><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)

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

任何帮助将不胜感激!

【问题讨论】:

【参考方案1】:

问题是您的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

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

【讨论】:

快速说明 - 我的调用实际上在我的 form.validate() 命令中失败了。我不确定flask是如何知道的,但它需要在它采取任何它应该采取的验证操作之后返回一些东西(即你必须不仅在form.validate()if语句中而且在它之后有一个返回) 【参考方案2】:

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

【讨论】:

以上是关于错误:函数返回 None 或在没有返回语句的情况下结束。但在哪里找不到? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

python基础:递归函数返回值(return)误区

函数返回 None 没有返回语句

当返回对象的函数在没有返回语句的情况下结束时会发生啥

在没有返回语句的情况下到达函数结尾

Python 函数return返回None?

Python函数