* 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'然而,无论出于什么原因,它并没有,500个我无法用Flask捕捉到的arrises。我只想说明URL不是有效的端点,并因此重定向到主页,但这似乎不起作用。
任何帮助都是非常感谢的。
发布于 2021-04-08 17:45:00
404错误处理程序必须能够接受一些参数。
@app.errorhandler(404)
def page_not_found(_):
print('hit')
return '400'发布于 2021-04-08 17:51:11
然而,一个平淡无奇的修复
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
print(f'recieved invalid request attempt of /{path}')
return render_template('redirect.html', reason='Unrecognised endpoint.')将此应用程序路由用作“总括”,以防止首先出现任何404错误。
https://stackoverflow.com/questions/67001386
复制相似问题