如何在 Python Bottle 中抛出自定义 404 消息

Pat*_*awa 2 python bottle

使用 Python 的 Bottle 框架,我试图捕获所有 404 并用我自己的输出替换框架的输出。

如何捕获所有抛出的 404 并将它们替换为我自己的输出?

到目前为止,我已经将我的应用程序简化为一个抛出 404 的应用程序,它仍然输出框架的“错误:404 未找到”输出,而不是“这里什么都没有,抱歉”。我的申请如下。

from bottle import Bottle, error

"""App Instantiation"""
app = application = Bottle()

@error(404)
def error404(error):
    return 'Nothing here, sorry'
Run Code Online (Sandbox Code Playgroud)

ron*_*man 5

使用@app.error.

from bottle import Bottle

"""App Instantiation"""
app = application = Bottle()

@app.error(404)  # changed from OP
def error404(error):
    return 'Nothing here, sorry'
Run Code Online (Sandbox Code Playgroud)

@error不会将您的error404功能绑定到您的应用程序。(我相信它只是将它绑定到 Bottle 的“默认”应用程序,IMO 是一个有点令人困惑和不必要的功能。)