简体   繁体   English

Django 带有压缩静态文件的白噪声

[英]Django Whitenoise with compressed staticfiles

I'm not able to get my django project to run with whitenoise and compressed staticfiles (including libsass).我无法让我的 django 项目使用白噪声和压缩静态文件(包括 libsass)运行。 In links below, I read that it's only possible by offline compression of needed staticfiles.在下面的链接中,我读到只有通过离线压缩所需的静态文件才有可能。 But when I started up the docker container, running compress command但是当我启动 docker 容器时,运行compress命令

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

gives me error:给我错误:

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

While trying to request the site gives me following error (as expected?):在尝试请求该站点时给我以下错误(如预期的那样?):

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

Settings are as follows (build with cookiecutter-django, see link for complete code base below):设置如下(使用 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

So after searching the internet for 1 day;所以在网上搜索了1天后; I'm stuck... Thx for any help or suggestion!我被卡住了......谢谢任何帮助或建议!

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

which is build with cookiecutter-django-foundation这是用cookiecutter-django-foundation构建的

including the following changes to config/setttings/production.py :包括对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

Possible related links:可能的相关链接:

EDIT编辑

Solved it using Justins answer (see below, with additonal changes).使用贾斯汀的答案解决了它(见下文,有额外的变化)。 My mistake was trying to compress files with already running container, giving me the error above.我的错误是试图用已经运行的容器压缩文件,给我上面的错误。 After changing Dockerfile with following lines (Notice the duplicate collectstatic cmd:):用以下几行更改 Dockerfile 后(注意重复的collectstatic cmd :):

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

and rebuilding image everything worked like a charm:) Additionally, diverging from settings above, I had to set COMPRESS_ENABLED=True in my settings/env file.和重建图像一切都像一个魅力:) 此外,与上面的设置不同,我必须在我的设置/环境文件中设置COMPRESS_ENABLED=True

I just had the same problem.我只是有同样的问题。

Add this to project/compose/production/django/start将此添加到project/compose/production/django/start

python /app/manage.py compress --force

ie IE

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

this is weird but it work very well.这很奇怪,但效果很好。

collect and compress static files by whitenoise通过whitenoise收集和压缩static文件

python manage.py collectstatic --clear

set COMPRESS_STORAGE = 'compressor.storage.BrotliCompressorFileStorage' to make.br files in CACHE directory将 COMPRESS_STORAGE = 'compressor.storage.BrotliCompressorFileStorage' 设置为 CACHE 目录中的 make.br 文件

python manage.py compress --force

set COMPRESS_STORAGE = 'compressor.storage.GzipCompressorFileStorage' to make.gzfiles in CACHE directory将 COMPRESS_STORAGE = 'compressor.storage.GzipCompressorFileStorage' 设置为 CACHE 目录中的 make.gzfiles

python manage.py compress --force

to add new compressed files to whitenoise: manifest.json, manifest.json.gz, manifest.json.br --no-post-process option is to tell whitenoise not to compress static files again. to add new compressed files to whitenoise: manifest.json, manifest.json.gz, manifest.json.br --no-post-process option is to tell whitenoise not to compress static files again.

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

make sure to run the commands in order.确保按顺序运行命令。

to test if whitenoise is working测试白噪声是否有效

python manage.py runserver --nostatic

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM