4

I'm not able to get my django project to run with whitenoise and compressed staticfiles (including 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-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):

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; I'm stuck... Thx for any help or suggestion!

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

which is build with cookiecutter-django-foundation

including the following changes to 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!):

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.

Henhuy
  • 1,034
  • 1
  • 13
  • 23
  • For me I had `COMPRESS_STORAGE = STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' ` from a former heroku installation... changing it to `COMPRESS_STORAGE = "compressor.storage.GzipCompressorFileStorage" STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'` solved my problem, thanks for the detailed configuration here which hepled me solving my issue :) – chakmear Oct 24 '22 at 07:54

2 Answers2

2

I just had the same problem.

Add this to project/compose/production/django/start

python /app/manage.py compress --force

i.e.

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
Justin
  • 36
  • 2
  • Hey Justin, thank you very much. For me it worked after including a second `python /app/manage.py collectstatic --noinput` after the compress command. Before, the files have been compressed, but whitenoise didn't see/accept them?! Now, everything works! – Henhuy Nov 22 '19 at 19:46
0

this is weird but it work very well.

collect and compress static files by whitenoise

python manage.py collectstatic --clear

set COMPRESS_STORAGE = 'compressor.storage.BrotliCompressorFileStorage' to make .br files in CACHE directory

python manage.py compress --force

set COMPRESS_STORAGE = 'compressor.storage.GzipCompressorFileStorage' to make .gzfiles in CACHE directory

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.

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
Mahdi Hamzeh
  • 289
  • 2
  • 8