Python 错误:函数返回None或结束时没有返回语句。但在哪里找不到?

Python 错误:函数返回None或结束时没有返回语句。但在哪里找不到?,python,html,sql,flask,Python,Html,Sql,Flask,我正试图根据用户填写的表单中提交的数据更新数据库数据,但我收到此错误: @app.route('/kudson/edit/<id>', methods=['POST', 'GET']) def editid(id): if request.method == 'POST': pass else: try: id = request.form['no']

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

@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)
[TypeError:view函数未返回有效响应。该函数要么未返回任何响应,要么在没有返回语句的情况下结束。]

错误堆栈跟踪:

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 width="50%"><input type="submit" value="submit"/></td>
    <tr></tr>
    <td><a href = "/kudson/update">กลับหน้าหลัก</a></td>
</center>
    </tbody>
</form>
</table>
</center>
</html>

ร้านสาขา {{行[2]}
ผลการแก้ไขข้อมูล: {{msg}}
PMA
UPSD
BPSD
เช้า
62
เช้า
87
บ่าย
62
บ่าย
87
ดึก
62
ดึก
87
这是我的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)
@app.route('/kudson/edit/',methods=['POST','GET']
def editid(id):
如果request.method==“POST”:
通过
其他:
尝试:
id=请求。表格['no']
日期=请求。表格['date']
mo_62_upsd=请求。表单['mo_62_upsd']
mo_62_bpsd=请求。表单['mo_62_bpsd']
mo_87_upsd=请求。表单['mo_87_upsd']
mo_87_bpsd=请求。表单['mo_87_bpsd']
af_62_upsd=请求。表单['af_62_upsd']
af_62_bpsd=请求。表单['af_62_bpsd']
af_87_upsd=请求。表单['af_87_upsd']
af_87_bpsd=请求。表格['af_87_bpsd']
ev_62_upsd=请求。表单['ev_62_upsd']
ev_62_bpsd=请求。表格['ev_62_bpsd']
ev_87_upsd=请求。表格['ev_87_upsd']
ev_87_bpsd=请求。表格['ev_87_bpsd']
使用sql.connect(con_字符串)作为con:
cur=con.cursor()
参数=日期、日期、日期、日期、日期、日期、日期、日期、日期、日期、日期
cur.execute(““”更新kudson\u shift\u daily\u sell
设置日期=?,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_87_upsd=%s,ev_87_bpsd=%s
其中id=%s“”,参数)
con.commit()
味精=”แก้ไขข้อมูลสำเร็จ"
返回render_模板(“kudson_edit.html”,msg=msg)
例外情况除外,如e:
返回render_模板(“kudson_edit.html”,msg=e)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app.run(debug=True)
如果代码看起来不干净,我很抱歉,我是一个编码新手


任何帮助都将不胜感激!

如果您正在执行
POST
请求,问题是您根本不返回任何内容,您只需通过
即可,代码将继续运行,但没有其他操作或返回,并且函数将在不返回接受值的情况下退出。您必须返回有效的HTTP响应

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

<form action ="{{ url_for('editid',id=rows[0])}}" method="POST">
但是您的
POST
路由没有返回有效的HTTP响应:

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

您需要决定在提交此表单数据时要对其执行什么操作。您可能需要验证它,将其保存在数据库中,并根据上述操作返回适当的重定向视图。

Quick note-我的呼叫在我的表单中实际失败。validate()我不知道flask是怎么知道的,但它要求在执行了它应该执行的任何验证操作之后返回某些内容(即,您不仅必须在form.validate()if语句中返回,而且还必须在它之后返回)