python manage.py collectstatic 错误:找不到 rest_framework bootstrap.min.css.map(来自“Django for APIs”一书)

回答 2 浏览 1212 2022-08-18

我正在阅读 "William S. Vincent "的《Django for APIs》一书(Django 4.0的当前版本)。

在第4章中,我不能成功地运行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

我找不到任何解释。也许有人能为我指出正确的方向。

Dan 提问于2022-08-18
你是否在你的INSTALLED_APPS上加了rest_frameworkJPG 2022-08-18
是的,它在我的INSTALLED_APPS中。Dan 2022-08-18
2 个回答
#1楼 已采纳
得票数 20

Update:DRF 3.14.0现在支持Django 4.1。如果你按照下面的方法给静态添加了存根,请务必将它们删除。

这似乎与Django 4.1有关:要么降级到Django 4.0,要么简单地在你的一个静态目录中创建以下的空文件

static/rest_framework/css/bootstrap-theme.min.css.map
static/rest_framework/css/bootstrap.min.css.map

ManifestStaticFilesStorage最近有一个变化,它现在尝试用散列的对应物来替换源map。

Django REST框架最近才添加了bootstrap css源码地图,但还没有发布。

shangxiao 提问于2022-08-19
shangxiao 修改于2022-09-24
谢谢!如果我改成Django 4.0,它就能工作了Dan 2022-08-19
对于任何未来的读者。现在仍然是这样的情况。AvidJoe 2022-09-20
最新版本的Django Rest Framework终于支持Django 4.1了 django-rest-framework.org/community/release-notes/#314x-seriesJon 2022-09-23
我使用的是Django 4.1.1,添加空文件对我来说是有效的。george_the_frog 2022-11-18
#2楼
得票数 0

你有STATIC_ROOT 指向BASE_DIR / "staticfiles",然后是STATIC_URL = "static/"。让它们像这样指向同一个静态文件夹。

STATIC_URL = '/static/'
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [

    os.path.join(BASE_DIR, "static")
]
sikaili99 提问于2022-08-18
如果我像这样改变设置,如果我设置STATIC_ROOT = BASE_DIR / "static",我会得到django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.,如果我设置STATIC_ROOT = BASE_DIR / "staticfiles",我会得到The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.,同样我会得到The CSS file 'rest_framework/css/bootstrap.min.css' references a file which could not be found: rest_framework/css/bootstrap.min.css.mapDan 2022-08-19
你使用的是哪个版本的Django,这个设置从第2版开始适用,我在第4.0版中也使用过。sikaili99 2022-08-19
我使用的是4.1版本。我现在用4.0版试了一下,可以工作了。Dan 2022-08-19