[Fixed]-Cache busting in Django 1.8?


Yes this can be done automatically with contrib.staticfiles. There are two additional provided storage classes which will rename files using a hash. These
are documented here: ManifestStaticFilesStorage and CachedStaticFilesStorage

From the docs:

A subclass of the StaticFilesStorage storage backend which stores the file names it handles by appending the MD5 hash of the file’s content to the filename. For example, the file css/styles.css would also be saved as css/styles.55e7cbb9ba48.css.

The purpose of this storage is to keep serving the old files in case some pages still refer to those files, e.g. because they are cached by you or a 3rd party proxy server. Additionally, it’s very helpful if you want to apply far future Expires headers to the deployed files to speed up the load time for subsequent page visits.

The main difference is

CachedStaticFilesStorage is a similar class like the ManifestStaticFilesStorage class but uses Django’s caching framework for storing the hashed names of processed files instead of a static manifest file called staticfiles.json. This is mostly useful for situations in which you don’t have access to the file system.

To enable them you need to change your STATICFILES_STORAGE setting is set to 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage' or 'django.contrib.staticfiles.storage.CachedStaticFilesStorage'. The file names are only changed when DEBUG=False as it would be in production.


I’m no expert in Caching, but I think letting nginx handle caching might be better than using Django. Django has a lot to handle, so you can let the lightweight static server do that nasty job.

I do not use cloudflare, but I use this line to cache my statics, however, immediately the file changes, Nginx propagates the most recent file (thats the same file):

location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 30d;

which is a snippet from this gist I’m currently using that conf in production, so I know it works.

One thing I’ll point out is, make sure MemCached isn’t working and connected to your django as a Caching Backend. I say this, because I have spent many hours in time past hitting my head against the wall, simply because MemCached was caching my page with every content in it for up to 10 minutes.

with this nginx location conf, Whenever I change my .css, or upload a new file (static), the new file immediately takes over, as long as I’ve python manage.py collectstatic‘ed them into the appropriate directory

I stand to be corrected though, if that’s not actually the part doing the trick.

Proof that above works with Cache-busting (as you call it)

  • I went into server
  • Deleted my static folder (nginx still running) sudo rm -rf static/
  • Accessed my site
  • No static loaded
  • Went back, and python manage.py collectstatic
  • Accessed my site again. Statics loaded
  • No browser hard refresh was used. No nginx reload|restart whatever was used.

Nginx is smart enough to cache your statics, but reload the static when the file is new and serve it.



If you use a CDN, cache busting becomes more complex. If you just want to bust the cache for a specific image, just create a simple tag that does the trick….

import time
from django import template

register = template.Library()

def cache_bust():
  return int(time.time())

Then in your template just do something like this…

{% load cache_app %}
<img src="/captcha/?cache_bust={% cache_bust %}" class="captcha"/>

And you have cache busting the simple way.

Leave a comment