TypeError: The view function did not return a valid response. The function either returned None 的解决
使用flask框架制作登录、注册的页面时,app.py运行成功,数据库有用户,1234,密码也是1234
点击登录之后, 报如下错误。
TypeError
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
页面截图如下:
查网上的 报错,解决办法是路由没有返回东西,于是我改了return语句,if和else都有返回值。
try:
# 执行sql语句
cursor.execute(sql)
results = cursor.fetchall()
print(len(results))
if len(results) == 1:
#return '登录成功'
return render_template('pingtable2.html')
else:
#return '用户名或密码不正确'
return render_template('pingtable2.html')
# 提交到数据库执行
db.commit()
但是再次运行的时候还是报同样的错误。
于是我看了以下python控制台。
Traceback (most recent call last):
File "D:\技术学习\卫生健康委员会\登录页面\cpe项目代码\登录界面\app.py", line 65, in getLoginRequest
cursor.execute(sql)
File "D:\python\2\lib\site-packages\pymysql\cursors.py", line 163, in execute
result = self._query(query)
File "D:\python\2\lib\site-packages\pymysql\cursors.py", line 321, in _query
conn.query(q)
File "D:\python\2\lib\site-packages\pymysql\connections.py", line 505, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "D:\python\2\lib\site-packages\pymysql\connections.py", line 724, in _read_query_result
result.read()
File "D:\python\2\lib\site-packages\pymysql\connections.py", line 1069, in read
first_packet = self.connection._read_packet()
File "D:\python\2\lib\site-packages\pymysql\connections.py", line 676, in _read_packet
packet.raise_for_error()
File "D:\python\2\lib\site-packages\pymysql\protocol.py", line 223, in raise_for_error
err.raise_mysql_exception(self._data)
File "D:\python\2\lib\site-packages\pymysql\err.py", line 107, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1")
127.0.0.1 – – [18/Apr/2022 16:53:14] "GET /login?user=1234&password=1234 HTTP/1.1" 500 –
Traceback (most recent call last):
File "D:\python\2\lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "D:\python\2\lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "D:\python\2\lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "D:\python\2\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "D:\python\2\lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "D:\python\2\lib\site-packages\flask\app.py", line 1953, in full_dispatch_request
return self.finalize_request(rv)
File "D:\python\2\lib\site-packages\flask\app.py", line 1968, in finalize_request
response = self.make_response(rv)
File "D:\python\2\lib\site-packages\flask\app.py", line 2098, 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.
127.0.0.1 – – [18/Apr/2022 16:53:14] "GET /login?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 –
127.0.0.1 – – [18/Apr/2022 16:53:14] "GET /login?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 –
127.0.0.1 – – [18/Apr/2022 16:53:14] "GET /login?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 –
127.0.0.1 – – [18/Apr/2022 16:53:14] "GET /login?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 –
127.0.0.1 – – [18/Apr/2022 16:53:14] "GET /login?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 –
注意到这个标红的地方,sql语句,我判断,可能是sql语句写错了,没办法往下执行。
db = pymysql.connect(host="localhost", user="root", password="1234", database="testdb") # 使用cursor()方法获取操作游标 cursor = db.cursor() # SQL 查询语句 sql = "select * from user where user=" + request.args.get('user') + " and password=" + request.args.get('password')+")"
我注意到sql语句后面有个多的括号,把他删除
再次运行:
成功跳到下一个页面了。
调错完毕。
来源:鹅鹅鹅鹅鹅鹅鹅鹅鹅鹅鹅鹅