'collectstatic' command fails when WhiteNoise is enabled

22,160

Solution 1

The problem here is that css/iconic/open-iconic-bootstrap.css is referencing a file, open-iconic.eot, which doesn't exist in the expected location.

When you run collectstatic with that storage backend Django attempts to rewrite all the URLs in your CSS files so they reference the files by their new names e.g, css/iconic/open-iconic.8a7442ca6bed.eot. If it can't find the file it stops with that error.

Solution 2

I just had this same issue and fixed it by removing this line from my settings file,

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

I got this line from the Heroku documentation page...

Solution 3

I've had this error claiming a missing .css file when all my .css files existed, because I trusted Heroku documentation:

STATIC_ROOT = 'staticfiles'

over WhiteNoise documentation:

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

The fix is trivial, but until Heroku fix their docs (I submitted feedback), lets make sure the solution at least appears in SO.

Solution 4

The issue here is that using

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

or

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage

uses Django's static file storage in a different way than runserver does. See the Django docs for some explanation: https://docs.djangoproject.com/en/1.11/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.ManifestStaticFilesStorage.manifest_strict

I believe the referenced manifest gets built when you run collectstatic, so doing so should fix this problem temporarily, but you likely don't want to run collectstatic before every test run if you have modified any static files. Another solution would be to disable this setting for your tests, and just run it in production.

Solution 5

In development Django’s runserver automatically takes over static file handling.

In most cases this is fine, however this means that some of the improvements that WhiteNoise makes to static file handling won’t be available in development and it opens up the possibility for differences in behaviour between development and production environments. For this reason it’s a good idea to use WhiteNoise in development as well.

You can disable Django’s static file handling and allow WhiteNoise to take over simply by passing the --nostatic option to the runserver command, but you need to remember to add this option every time you call runserver. An easier way is to edit your settings.py file and add whitenoise.runserver_nostatic to the top of your INSTALLED_APPS list:

INSTALLED_APPS = [
'whitenoise.runserver_nostatic',
'django.contrib.staticfiles',
# ...]

Source - http://whitenoise.evans.io/en/stable/django.html#using-whitenoise-in-development

Share:
22,160
Pieter
Author by

Pieter

Updated on August 29, 2020

Comments

  • Pieter
    Pieter over 3 years

    I'm trying to serve static files through WhiteNoise as per Heroku's recommendation. When I run collectstatic in my development environment, this happens:

    Post-processing 'css/iconic/open-iconic-bootstrap.css' failed!
    
    Traceback (most recent call last):
      File "./manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
      File "/home/Pieter/.virtualenvs/radiant/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
        utility.execute()
      File "/home/Pieter/.virtualenvs/radiant/lib/python3.4/site-packages/django/core/management/__init__.py", line 377, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/home/Pieter/.virtualenvs/radiant/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv
        self.execute(*args, **options.__dict__)
      File "/home/Pieter/.virtualenvs/radiant/lib/python3.4/site-packages/django/core/management/base.py", line 338, in execute
        output = self.handle(*args, **options)
      File "/home/Pieter/.virtualenvs/radiant/lib/python3.4/site-packages/django/core/management/base.py", line 533, in handle
        return self.handle_noargs(**options)
      File "/home/Pieter/.virtualenvs/radiant/lib/python3.4/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 168, in handle_noargs
        collected = self.collect()
      File "/home/Pieter/.virtualenvs/radiant/lib/python3.4/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 120, in collect
        raise processed
      File "/home/Pieter/.virtualenvs/radiant/lib/python3.4/site-packages/django/contrib/staticfiles/storage.py", line 242, in post_process
        content = pattern.sub(converter, content)
      File "/home/Pieter/.virtualenvs/radiant/lib/python3.4/site-packages/django/contrib/staticfiles/storage.py", line 181, in converter
        hashed_url = self.url(unquote(joined_result), force=True)
      File "/home/Pieter/.virtualenvs/radiant/lib/python3.4/site-packages/django/contrib/staticfiles/storage.py", line 128, in url
        hashed_name = self.stored_name(clean_name)
      File "/home/Pieter/.virtualenvs/radiant/lib/python3.4/site-packages/django/contrib/staticfiles/storage.py", line 277, in stored_name
        cache_name = self.clean_name(self.hashed_name(name))
      File "/home/Pieter/.virtualenvs/radiant/lib/python3.4/site-packages/django/contrib/staticfiles/storage.py", line 91, in hashed_name
        (clean_name, self))
    ValueError: The file 'css/fonts/open-iconic.eot' could not be found with <whitenoise.django.GzipManifestStaticFilesStorage object at 0x7f57fc5b1550>.
    

    The static collection command runs without incident when I comment out this line in my settings:

    STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
    

    What's going wrong here and how do I fix it? I already tried emptying my static file output folder. It runs smoothly until it starts processing one specific file.