python config_error ai_generated true

文件未找到错误:没有这样的文件或目录:'static/style.css'

FileNotFoundError: [Errno 2] No such file or directory: 'static/style.css'

ID: python/starlette-missing-static-files

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

版本兼容性

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

根因分析

Starlette 静态文件挂载要求目录存在;如果文件丢失或路径错误,则会发生此错误。

English

Starlette static file mount expects the directory to exist; if the file is missing or path is incorrect, this error occurs.

generic

解决方案

  1. 100% 成功率 Create the static directory and place files
    mkdir -p static
    # place style.css in static/
  2. 95% 成功率 Mount static files with correct path
    from starlette.staticfiles import StaticFiles
    app.mount('/static', StaticFiles(directory='static'), name='static')

无效尝试

常见但无效的做法:

  1. Using a relative path that doesn't exist 90% 失败

    The path must be absolute or relative to the current working directory.

  2. Not mounting the static files at all 100% 失败

    Static files won't be served.