python runtime_error ai_generated true

Starlette HTTP异常:404 未找到

starlette.exceptions.HTTPException: 404 Not Found

ID: python/starlette-http-exception-404

其他格式: JSON · Markdown 中文 · English
80%修复率
82%置信度
0证据数
2024-05-10首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

请求的URL没有定义路由,或由于路由不匹配导致URL错误。

English

A route is not defined for the requested URL, or the URL is incorrect due to routing mismatches.

generic

解决方案

  1. 90% 成功率
    Check app.routes to see registered routes and ensure the path matches exactly, including trailing slashes.
  2. 80% 成功率
    Add a catch-all route: @app.route('/{path:path}')\nasync def catch_all(request):\n    raise HTTPException(status_code=404)

无效尝试

常见但无效的做法:

  1. 50% 失败

    Catching all HTTPExceptions and returning a custom page can mask other errors like 500s.

  2. 70% 失败

    Using regex patterns incorrectly in route paths can cause unexpected 404s.