staticfiles ManifestStaticFilesStorage and Form Media

54 views
Skip to first unread message

Brian Neal

unread,
Dec 6, 2015, 4:17:20 PM12/6/15
to Django users
I'm investigating using staticfiles ManifestStaticFilesStorage. Unless I'm missing something, there seems to be a hole with Django forms Media class. If you use an inner Media class you do not get the hashed versions of the assets filenames. 

This leads me to believe you can't use the Forms Media technique with ManifestStaticFilesStorage. 

I noticed there was an undocumented function called static in staticfiles.templatetags.staticfiles. I tried calling this function inside my inner Media classes to make it return the hashed version of the files, and this does work as long as collectstatic has been run. But if you have an empty STATIC_DIR, you can't even call the management command to run collectstatic as it tries to resolve these Media classes at import time. So it seems to be a chicken and egg problem. A possible work around was found on stackoverflow to delay the evaluation of the Media classes: http://stackoverflow.com/questions/28366281/django-cachedstaticfilesstorage-in-modeladmin-media

So it seems I have a couple of options:

1) Give up on form Media classes and explicitly use {% static %} in my templates instead of {% form.media %}
2) Defer the resolution of {% form.media %} using the stackoverflow trick

Since I'm not doing any dynamic decision making in my views about what forms to use (yet), I always know what static files I need in my templates to support my forms, so I'm leaning toward option 1. But just wanted to bounce this off the community in case I am missing something.

Thanks.

Denis Cornehl

unread,
Dec 7, 2015, 6:42:23 AM12/7/15
to django...@googlegroups.com
Hi Brian, 

a sidenote, this will be fixed: 

-- 
Denis
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/39a230f1-3025-4964-b543-b7d48a0798ab%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brian Neal

unread,
Dec 8, 2015, 12:51:49 AM12/8/15
to Django users

Thank you Denis. I will keep my eye on this ticket: https://code.djangoproject.com/ticket/21221

Best,
BN
Reply all
Reply to author
Forward
0 new messages