【发布时间】:2021-07-04 04:04:36
【问题描述】:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [08/Apr/2021 10:38:05] "GET / HTTP/1.1" 200 -
[2021-04-08 10:38:08,866] ERROR in app: Exception on /cap [GET]
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1926, in dispatch_request
self.raise_routing_exception(req)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1908, in raise_routing_exception
raise request.routing_exception
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/ctx.py", line 350, in match_request
result = self.url_adapter.match(return_rule=True)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/werkzeug/routing.py", line 1945, in match
raise NotFound()
werkzeug.exceptions.NotFound: 404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1816, in handle_user_exception
return self.handle_http_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1744, in handle_http_exception
return handler(e)
TypeError: not_found() takes 0 positional arguments but 1 was given
127.0.0.1 - - [08/Apr/2021 10:38:08] "GET /cap HTTP/1.1" 500 -
127.0.0.1 - - [08/Apr/2021 10:38:08] "GET /favicon.ico HTTP/1.1" 200 -
/cap 不是有效的端点。当我尝试获取它时,它肯定会返回错误 404,如最初的错误消息所示,并且此代码块应该可以工作。
@app.errorhandler(404)
def page_not_found():
print('hit')
return '400'
但是,无论出于何种原因,它都没有,并且我无法用 Flask 捕捉到 500 个。我只想说明无效端点的 URL 并因此重定向到主页,但这似乎不起作用。
非常感谢任何帮助。
【问题讨论】:
标签: python flask error-handling http-status-code-404