使用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:pythonlibsite-packagespymysqlcursors.py”, line 163, in execute
result = self._query(query)
File “D:pythonlibsite-packagespymysqlcursors.py”, line 321, in _query
conn.query(q)
File “D:pythonlibsite-packagespymysqlconnections.py”, line 505, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File “D:pythonlibsite-packagespymysqlconnections.py”, line 724, in _read_query_result
result.read()
File “D:pythonlibsite-packagespymysqlconnections.py”, line 1069, in read
first_packet = self.connection._read_packet()
File “D:pythonlibsite-packagespymysqlconnections.py”, line 676, in _read_packet
packet.raise_for_error()
File “D:pythonlibsite-packagespymysqlprotocol.py”, line 223, in raise_for_error
err.raise_mysql_exception(self._data)
File “D:pythonlibsite-packagespymysqlerr.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 /loginuser=1234&password=1234 HTTP/1.1” 500 -
Traceback (most recent call last):
File “D:pythonlibsite-packages laskapp.py”, line 2464, in __call__
return self.wsgi_app(environ, start_response)
File “D:pythonlibsite-packages laskapp.py”, line 2450, in wsgi_app
response = self.handle_exception(e)
File “D:pythonlibsite-packages laskapp.py”, line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File “D:pythonlibsite-packages lask_compat.py”, line 39, in reraise
raise value
File “D:pythonlibsite-packages laskapp.py”, line 2447, in wsgi_app
response = self.full_dispatch_request()
File “D:pythonlibsite-packages laskapp.py”, line 1953, in full_dispatch_request
return self.finalize_request(rv)
File “D:pythonlibsite-packages laskapp.py”, line 1968, in finalize_request
response = self.make_response(rv)
File “D:pythonlibsite-packages laskapp.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语句后面有个多的括号,把他删除
再次运行:
成功跳到下一个页面了。
调错完毕。
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦