python
config_error
ai_generated
true
INFO: 127.0.0.1:xxxxx - "GET /static/style.css HTTP/1.1" 404 未找到
INFO: 127.0.0.1:xxxxx - "GET /static/style.css HTTP/1.1" 404 Not Found
ID: python/starlette-static-files-404
80%修复率
86%置信度
0证据数
2024-05-21首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 0.30+ | active | — | — | — |
根因分析
StaticFiles 挂载的目录相对于进程工作目录不存在,或挂载路径与 URL 前缀不一致。
English
StaticFiles was mounted with a directory that does not exist relative to the process working directory, or the mount path and URL prefix disagree.
解决方案
-
95% 成功率
from pathlib import Path from fastapi.staticfiles import StaticFiles BASE = Path(__file__).resolve().parent app.mount('/static', StaticFiles(directory=BASE / 'static'), name='static') -
85% 成功率
assert (BASE / 'static').is_dir(), 'static dir missing'
无效尝试
常见但无效的做法:
-
80% 失败
Bypasses StaticFiles caching and content-type handling; fragile.
-
75% 失败
Shadows API routes registered after the mount.