如何制作自定义 404 页面?

AshKetchumPL

我正在为不和谐创建一个 rick roll 站点,我想在404响应状态代码上重定向到 rick roll。

我尝试了以下方法,但没有奏效:

 @app.exception_handler(fastapi.HTTPException)
 async def http_exception_handler(request, exc):
     ...
克里斯

您需要创建一个middleware检查status_code. response如果是404,则返回RedirectResponse例子:

from fastapi.responses import RedirectResponse

@app.middleware("http")
async def redirect_on_not_found(request: Request, call_next):
    response = await call_next(request)
    if response.status_code == 404:
        return RedirectResponse("https://fastapi.tiangolo.com")
    else:
        return response

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章