执行collectstatic命令报错:找不到rest_framework的bootstrap.min.css.map
Django collectstatic 执行失败:MissingFileError 解决方向
执行python manage.py collectstatic时出现如下报错:
Traceback (most recent call last): File "/Users/my_name/Projects/django/django_for_apis/library/manage.py", line 22, in <module> main() File "/Users/my_name/Projects/django/django_for_apis/library/manage.py", line 18, in main execute_from_command_line(sys.argv) File "/Users/my_name/Projects/django/django_for_apis/library/.venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line utility.execute() File "/Users/my_name/Projects/django/django_for_apis/library/.venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Users/my_name/Projects/django/django_for_apis/library/.venv/lib/python3.10/site-packages/django/core/management/base.py", line 402, in run_from_argv self.execute(*args, **cmd_options) File "/Users/my_name/Projects/django/django_for_apis/library/.venv/lib/python3.10/site-packages/django/core/management/base.py", line 448, in execute output = self.handle(*args, **options) File "/Users/my_name/Projects/django/django_for_apis/library/.venv/lib/python3.10/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 209, in handle collected = self.collect() File "/Users/my_name/Projects/django/django_for_apis/library/.venv/lib/python3.10/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 154, in collect raise processed whitenoise.storage.MissingFileError: The file 'rest_framework/css/bootstrap.min.css.map' could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage object at 0x102fa07f0>. The CSS file 'rest_framework/css/bootstrap.min.css' references a file which could not be found: rest_framework/css/bootstrap.min.css.map Please check the URL references in this CSS file, particularly any relative paths which might be pointing to the wrong location.
当前settings.py中静态文件配置如下:
STATIC_URL = "static/" STATICFILES_DIRS = [BASE_DIR / "static"] # new STATIC_ROOT = BASE_DIR / "staticfiles" # new STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage" # new
以下是几个可行的解决方向:
- 临时替代存储类:将
STATICFILES_STORAGE临时改为django.contrib.staticfiles.storage.StaticFilesStorage,执行collectstatic完成后再改回原配置。此方法跳过了Whitenoise的压缩和manifest生成,可快速完成当前步骤,但不适合生产环境。 - 匹配DRF版本:报错原因是DRF的静态文件中bootstrap.min.css引用了不存在的map文件,大概率是当前DRF版本与书中示例不匹配。安装书中对应的DRF版本(适配Django4.0的版本通常为3.13.x系列)即可解决。
- 手动补充缺失文件:从Bootstrap官方下载对应版本的
bootstrap.min.css.map文件,放置到虚拟环境内DRF的静态文件目录rest_framework/css/下,或者项目的static/rest_framework/css/目录中。 - 自定义存储类忽略检查:编写自定义存储类继承
CompressedManifestStaticFilesStorage,覆盖相关方法忽略CSS映射文件的缺失检查,示例代码如下:
然后将from whitenoise.storage import CompressedManifestStaticFilesStorage from whitenoise.storage import MissingFileError class IgnoreMissingMapFilesStorage(CompressedManifestStaticFilesStorage): def hashed_name(self, name, content=None, filename=None): try: return super().hashed_name(name, content, filename) except MissingFileError: return nameSTATICFILES_STORAGE改为这个自定义类的路径(比如yourapp.storage.IgnoreMissingMapFilesStorage)。
内容的提问来源于stack exchange,提问作者Dan
相关产品推荐
相关产品推荐

