python
network_error
ai_generated
true
Starlette HTTP 异常:404 未找到。
starlette.exceptions.HTTPException: 404 Not Found
ID: python/starlette-http-exception-not-found
80%修复率
82%置信度
0证据数
2024-07-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
请求的 URL 与 Starlette 应用中的任何路由不匹配。
English
Requested URL does not match any route in the Starlette application.
解决方案
-
90% 成功率
from starlette.applications import Starlette from starlette.routing import Route async def homepage(request): return Response('Hello') routes = [Route('/', homepage)] app = Starlette(routes=routes) # Ensure route paths are correct with trailing slashes -
85% 成功率
Add a custom 404 handler: from starlette.exceptions import HTTPException from starlette.responses import JSONResponse async def not_found(request, exc): return JSONResponse(status_code=404, content={"message": "Not Found"}) app.add_exception_handler(HTTPException, not_found)
无效尝试
常见但无效的做法:
-
75% 失败
Catch-all routes may shadow specific routes if defined first.
-
60% 失败
Middleware cannot easily modify response status; may cause recursion.