我正在尝试根据用户填写的表单提交的数据更新数据库数据,但是我收到此错误:
[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 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)
对不起,如果代码看起来不干净,我是编码方面的新手。
任何帮助将不胜感激!
答案 0 :(得分:0)
如果您正在执行POST
请求,则问题在于您根本不返回任何内容,而您仅拥有一个pass
,代码将继续运行,但是没有其他事情要做或返回并退出该函数,而不返回可接受的值。您必须返回有效的HTTP响应。
答案 1 :(得分:0)
问题是您的POST
响应无效。正如@Shinra指出的那样,您只需在POST处理程序中执行pass
,这这不是有效的HTTP响应。发生这种情况是因为在您的表单执行时,它已被定向到您的editid
路由:
<form action ="{{ url_for('editid',id=rows[0])}}" method="POST">
然后将您的'editid
'路由设置为同时处理POST
和GET
路由:
@app.route('/kudson/edit/<id>', methods=['POST', 'GET'])
但是您的POST
路由未返回有效的HTTP响应:
def editid(id):
if request.method == 'POST':
pass
提交表单数据时,您需要决定要处理的数据。您可能想要验证它,将其保存在数据库中,并根据上述操作返回适当的重定向视图。