Django Whitenoise与压缩静态文件



我无法使用whiteoise和压缩静态文件(包括libsass(运行我的django项目。在下面的链接中,我读到只有离线压缩所需的静态文件才能实现。但是当我启动docker容器时,运行compress命令

docker-compose -f production.yml run --rm django python manage.py compress

给我错误:

ValueError: Missing staticfiles manifest entry for 'sass/app.scss'

在尝试请求网站时,我收到以下错误(如预期?(:

compressor.exceptions.OfflineGenerationError: You have offline compression enabled but key "..." is missing from offline manifest. You may need to run "python manage.py compress"

设置如下(使用cookiecutter django构建,请参阅下面完整代码库的链接(:

STATIC_ROOT = str(ROOT_DIR("staticfiles"))
STATIC_URL = "/static/"
STATICFILES_DIRS = [str(APPS_DIR.path("static"))]
STATICFILES_FINDERS = [
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
]
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
STATICFILES_FINDERS += ["compressor.finders.CompressorFinder"]
COMPRESS_PRECOMPILERS = [("text/x-scss", "django_libsass.SassCompiler")]
COMPRESS_CACHEABLE_PRECOMPILERS = (("text/x-scss", "django_libsass.SassCompiler"),)
COMPRESS_ENABLED = env.bool("COMPRESS_ENABLED", default=True)
COMPRESS_STORAGE = "compressor.storage.GzipCompressorFileStorage"
COMPRESS_URL = STATIC_URL

所以在互联网上搜索了1天后;我被卡住了。。。谢谢你的帮助或建议!

代码库:https://github.com/rl-institut/E_Metrobus/tree/compress

它是用cookiecutter django基金会构建的

包括对config/setttings/production.py的以下更改:

COMPRESS_STORAGE = "compressor.storage.GzipCompressorFileStorage"  # Instead of pre-set "storages.backends.s3boto3.S3Boto3Storage"
COMPRESS_ROOT = STATIC_ROOT  # Just in case
COMPRESS_OFFLINE = True  # Needed to run compress offline

可能的相关链接:

  • Whitenoise和django压缩器导致压缩文件的404
  • 可以在Django Compressor中使用WhiteNoise吗
  • 在Heroku上找不到Django静态文件(带有whiteoise(
  • https://github.com/django-compressor/django-compressor/issues/486

编辑

使用Justins答案解决了这个问题(见下文,有其他更改(。我的错误是试图用已经运行的容器压缩文件,导致上面的错误。用以下行更改Dockerfile后(注意重复的collectstaticcmd!(:

python /app/manage.py collectstatic --noinput
python /app/manage.py compress --force
python /app/manage.py collectstatic --noinput
/usr/local/bin/gunicorn config.wsgi --bind 0.0.0.0:5000 --chdir=/app

重建图像一切都像一种魅力:(此外,与上面的设置不同,我必须在设置/env文件中设置COMPRESS_ENABLED=True

我刚刚遇到了同样的问题。

将其添加到项目/合成/生产/django/start

python /app/manage.py compress --force

python /app/manage.py collectstatic --noinput
python /app/manage.py compress --force
/usr/local/bin/gunicorn config.wsgi --bind 0.0.0.0:5000 --chdir=/app

这很奇怪,但效果很好。

使用白化收集和压缩静态文件

python manage.py collectstatic --clear

set COMPRESS_STOAGE="compressor.STORAGE.BrotiCompressorFileStorage"在CACHE目录中制作.br文件

python manage.py compress --force

set COMPRESS_STOAGE="compressor.STORAGE.GzipCompressorFileStorage"在CACHE目录中制作.gz文件

python manage.py compress --force

向whiteoise添加新的压缩文件:manifest.json、manifest.json.gz,manifest.json。br--没有后处理选项是告诉whiteoise不要再压缩静态文件。

python manage.py collectstatic --no-post-process

确保按顺序运行命令。

测试白噪声是否工作

python manage.py runserver --nostatic

最新更新