CachedStaticFilesStorage and collectstatic for the very first time

329 views
Skip to first unread message

Andrzej Mateja

unread,
Jan 8, 2016, 9:19:30 AM1/8/16
to Django users
Hi,

I have a problem with using CachedStaticFilesStorage as a STATICFILES_STORAGE and with external package containing its own static files (easy_select2 to be specific). If I deploy my project for the very first time with DEBUG = False, empty cache and no external package static files copied to STATIC_ROOT I got an exception: ValueError: The file 'easy_select2/js/init.js' could not be found with <django.contrib.staticfiles.storage.CachedStaticFilesStorage object at 0xffffffffffff>. with every manage.py command, especially collectstatic. I understand that behaviour of HashedFilesMixin is legit but I'm not so sure if it should be like that when I want to collect my static files.

As a workaround I set temporally DEBUG = True, run manage.py collectstatic, set DEBUG back to False and do the rest. But I have a feeling that it is not the right way to do it especially because there is no problem when StaticFilesStorage is used - no need to do such tricks.

Best regards
Andrzej

Simon Charette

unread,
Jan 8, 2016, 1:31:17 PM1/8/16
to Django users
Hi Andrzej,

CachedStaticFilesStorage requires you run `collectstatic` before deploying
your application in production (non-DEBUG) mode.

The deployment should look like that:

1) Update your code to the latest version.
2) Run `collectstatic` to cache paths to hashed versions of your assets.
3) Restart your server.

Simon

Andrzej Mateja

unread,
Jan 8, 2016, 3:36:54 PM1/8/16
to Django users
Thank you Simon for sharing your remarks although it is not the case what you described. I'm talking about deploying brand new project which is depending on external application with its own static files. Project should have also no static files collected yet, static files cashing enabled (using CachedStaticFilesStorage) and empty cache. In such scenario I always get ValueError exception in hashed_name method from HashedFilesMixin class. I'm never able to complete collectstatic with DEBUG = False. My workaround is:
1. set DEBUG = True
2. run python manage.py collectstatic
3. set DEBUG = False
4. run python manage.py collectstatic again

In step 2. all static files from external application are copied to STATIC_ROOT which enables me to proceed. This workaround is not very beautiful nor django-like. Why? Because there is no such behaviour when I fell back to default StaticFilesStorage.

Andrzej

Simon Charette

unread,
Jan 8, 2016, 10:20:28 PM1/8/16
to Django users
Hi Andrzej,

If I understand correctly you can't run `collectstatic` with `DEBUG = False`
if you didn't run it with `DEBUG = True` beforehand.

I can't be sure without the full traceback but I suspect one of your
applications calls `django.contrib.staticfiles.storage.staticfiles_storage.url`
at module level triggering the `ValueError` since the hashed path to the
specified asset has not been computed and cached yet.

If you want to use `CachedStaticFilesStorage` you'll have to make sure not to
issue such calls in modules loaded when `collectstatic` runs.

Could you possible share the full traceback of the error raised when
`collectstatic --traceback` is run.

Simon

Andrzej Mateja

unread,
Jan 11, 2016, 5:59:57 AM1/11/16
to Django users
Hi Simon,

You are right. Package easy_select2 calls the url method, which you can see here:
https://github.com/asyncee/django-easy-select2/blob/master/easy_select2/widgets.py#L32

I'm going to report them that problem and I'll try also find a solution.

Cheers
Andrzej
Reply all
Reply to author
Forward
0 new messages