You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用WhiteNoise托管静态文件时执行collectstatic报错求助

问题描述

使用WhiteNoise托管静态文件时,执行python manage.py collectstatic命令出现以下错误:

whitenoise.storage.MissingFileError: The file 'vendor/bootswatch/default/bootstrap.min.css.map' could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage object at 0x0000029983D66570>.

移除所有WhiteNoise配置后,collectstatic可正常收集所有静态文件。当前settings.py中的相关配置如下:

静态文件配置

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/

STATIC_URL = 'static/'

STATICFILES_DIRS = [BASE_DIR /'static']

STATIC_ROOT = "static_root"

中间件配置

MIDDLEWARE = [    
    'django.middleware.security.SecurityMiddleware',    
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

存储配置

STORAGES = {    

    "staticfiles": {
        "BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
    },
}
解决方案
  • 检查目标文件是否存在
    确认本地static/vendor/bootswatch/default/目录下是否存在bootstrap.min.css.map文件。CompressedManifestStaticFilesStorage会严格校验CSS文件中引用的sourcemap文件是否存在,而默认Django静态文件存储不会做这个校验,这就是移除WhiteNoise后能正常执行的原因。

  • 若文件不存在,可选择以下方式处理

    1. 编辑bootstrap.min.css,找到末尾的/*# sourceMappingURL=bootstrap.min.css.map */,将其注释或删除,消除对map文件的引用
    2. 下载对应版本的bootstrap.min.css.map文件,放到static/vendor/bootswatch/default/目录中
    3. 更换WhiteNoise存储后端,改用whitenoise.storage.CompressedStaticFilesStorage,该后端不会生成静态文件manifest,也就不会校验依赖文件。修改STORAGES配置如下:
      STORAGES = {    
          "staticfiles": {
              "BACKEND": "whitenoise.storage.CompressedStaticFilesStorage",
          },
      }
      
  • 验证静态文件目录配置
    确认STATICFILES_DIRS中的BASE_DIR / 'static'路径正确,检查是否存在拼写错误(比如路径拼接时的空格问题),确保WhiteNoise能正确扫描到静态文件目录中的内容。

内容的提问来源于stack exchange,提问作者Azpect

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 12:57:21