python
network_error
ai_generated
true
starlette.exceptions.HTTPException: 404 Not Found
ID: python/starlette-http-exception-not-found
80%Fix Rate
82%Confidence
0Evidence
2024-07-10First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
Requested URL does not match any route in the Starlette application.
generic中文
请求的 URL 与 Starlette 应用中的任何路由不匹配。
Workarounds
-
90% success
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% success
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)
Dead Ends
Common approaches that don't work:
-
75% fail
Catch-all routes may shadow specific routes if defined first.
-
60% fail
Middleware cannot easily modify response status; may cause recursion.