Trying to set up hashed versioning in Django for static files

1,108 views
Skip to first unread message

Vernon Burt

unread,
Mar 6, 2014, 11:08:07 PM3/6/14
to django...@googlegroups.com
Hello all,

I've pretty quikly found out in my DJango adventure that I need to have some kind of versioning for my static css and javascript files. After talking to some web developers I was directed to use Django's CachedStaticFilesStorage app to append an md5 hash to the static files path. No matter how I set my static settings the files don't seem to get the has appended and  I can no longer link to any of my static files, either directly or using the static directive. I was wondering if anyone might know what I am doing wrong.

My  Settings:

STATIC_ROOT = 'staticfiles'

STATIC_URL = ''

# Additional locations of static files
STATICFILES_DIRS = (
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

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

My Template entry:
{% block cssfiles %}
{% load static%}
<link href="{% static "/static/css/map.css" %}" media="screen">
{% endblock %}
Anything that can point me in the right direction would help.If intrested, I've also had this question up on StackOverflow for a few days: 
http://stackoverflow.com/questions/22130697/djangos-cachedstaticfilesstorage-not-hashing-file-urls 

Camilo Torres

unread,
Mar 7, 2014, 7:15:11 PM3/7/14
to django...@googlegroups.com
Hello,

You should put a path in your STATIC setting:

Your static path should be pointing to a real path accessible from  the path you are running your app.

Camilo.

Tom Evans

unread,
Mar 8, 2014, 8:44:01 PM3/8/14
to django...@googlegroups.com
On Fri, Mar 7, 2014 at 4:08 AM, Vernon Burt <charg...@gmail.com> wrote:
> Hello all,
>
> I've pretty quikly found out in my DJango adventure that I need to have some
> kind of versioning for my static css and javascript files. After talking to
> some web developers I was directed to use Django's CachedStaticFilesStorage
> app to append an md5 hash to the static files path. No matter how I set my
> static settings the files don't seem to get the has appended and I can no
> longer link to any of my static files, either directly or using the static
> directive. I was wondering if anyone might know what I am doing wrong.
>
> My Settings:
>
> STATIC_ROOT = 'staticfiles'
>
> STATIC_URL = ''

This doesn't look right. STATIC_ROOT is the name of the directory that
django copies the static files to when you run collectstatic. This
should probably be an absolute path to avoid problems when django is
run from a different working directory.

STATIC_URL is the URL that you have configured your webserver to serve
the files that django copied in to STATIC_ROOT. It should *NOT* be
empty. Most people put it to '/static/'.

Two of the conditions for CachedStaticFilesStorage are that
DEBUG=False and that you have re-run collectstatic? If you have done
so, is STATIC_ROOT full of lovely files with md5 hashes in their
names?

Remember you will need to run collectstatic every time you change a file.


>
> # Additional locations of static files
> STATICFILES_DIRS = (
> )
>
> STATICFILES_FINDERS = (
> 'django.contrib.staticfiles.finders.FileSystemFinder',
> 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
> )
>
> STATICFILES_STORAGE =
> 'django.contrib.staticfiles.storage.CachedStaticFilesStorage'
>
>
> My Template entry:
>
> {% block cssfiles %}
> {% load static%}
> <link href="{% static "/static/css/map.css" %}" media="screen">
> {% endblock %}

So this is intended to find a file in "my_app_name/static/static/css/map.css"?

This is just a tip - always put the name of your app as the first part
of the static name, so "my_app_name/static/my_app_name/css/map.css",
referred to in the template as "{% static 'my_app_name/css/map.css'
%}". This avoids conflicts with other apps that might have a map.css.

Cheers

Tom

Vernon Burt

unread,
Mar 9, 2014, 9:02:23 PM3/9/14
to django...@googlegroups.com
I suppose that's my confusion - before this, each application directory had it's own /static/ directory with /css/, /js/ and /img/ directories as needed. I adjusted the settings as mentioned and tried the following:

DEBUG = False
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = ( os.path.join(PROJECT_PATH, 'static'),)
 

CollectStatic now works properly - it collects everything into the project root under static. File linking isn't though - the links dont work, and so none of the css or javascript loads. I'm wondering if I'm misusing the css tags I'm testing with. An example from my base template:

{% load static %}
    <link href="{% static '/css/custom.css' %}" media="screen">

Thank you for your time,

Vernon Burt

Tom Evans

unread,
Mar 10, 2014, 9:02:30 AM3/10/14
to django...@googlegroups.com
On Mon, Mar 10, 2014 at 1:02 AM, Vernon Burt <charg...@gmail.com> wrote:
> I suppose that's my confusion - before this, each application directory had
> it's own /static/ directory with /css/, /js/ and /img/ directories as
> needed. I adjusted the settings as mentioned and tried the following:

Yes - that is how it should be, apart from for project static files,
if you have any of those. You do not move the static files out of the
applications.

>
> DEBUG = False
> PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
> STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
> STATIC_URL = '/static/'
> STATICFILES_DIRS = ( os.path.join(PROJECT_PATH, 'static'),)

So, a slight problem here. STATIC_ROOT and STATICFILES_DIRS should be different.

STATICFILES_DIRS is a list of _additional_ directories that are copied
to STATIC_ROOT when you run collectstatic. This is where you list your
project static file directories, if you have any. You do not _need_ to
have any project static files, if all your static files are in
my_app/static, then you do not need to put anything in
STATICFILES_DIRS.

STATIC_ROOT is where files are collected to, IE it should be an empty
folder, it should not be the origin of your static files, it is where
your static files are copied to so that they can be efficiently
delivered to your users.

>
>
> CollectStatic now works properly - it collects everything into the project
> root under static. File linking isn't though - the links dont work, and so
> none of the css or javascript loads. I'm wondering if I'm misusing the css
> tags I'm testing with. An example from my base template:
>
> {% load static %}
> <link href="{% static '/css/custom.css' %}" media="screen">
>
> Thank you for your time,
>

Have you configured apache or nginx (or whatever you use) to serve the
directory STATIC_ROOT at the url STATIC_URL?

Cheers

Tom

Vernon Burt

unread,
Mar 11, 2014, 12:02:54 AM3/11/14
to django...@googlegroups.com, teva...@googlemail.com
That seemed to do it.. I definitly learned a lot about serving these files. Thank you to both Tom and Camilo, you helped me a lot!

Thanks again,

Vernon Burt
Reply all
Reply to author
Forward
0 new messages