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

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

  1. 95% 成功率
    from pathlib import Path
    from fastapi.staticfiles import StaticFiles
    BASE = Path(__file__).resolve().parent
    app.mount('/static', StaticFiles(directory=BASE / 'static'), name='static')
  2. 85% 成功率
    assert (BASE / 'static').is_dir(), 'static dir missing'

无效尝试

常见但无效的做法:

  1. 80% 失败

    Bypasses StaticFiles caching and content-type handling; fragile.

  2. 75% 失败

    Shadows API routes registered after the mount.