Index ¦ Archives ¦ Atom

Speed up Django static files

For a fairly easy performance win, and for something which makes dealing with old cached CSS something a thing of the past - enable ManifestStaticFilesStorage.

First of all you'll need to edit

# Use manifest static storage

Then you'll need to edit any templates which aren't using the static template tag. Instead of using:

<img src="{{ STATIC_URL }}images/hello.jpg" %}" alt="Hello">

You'll need to use:

{% load static from staticfiles %}
<img src="{% static 'images/hello.jpg' %}" alt="Hello">

Now when you run django-admin collectstatic, Django will include the MD5 hash of the file as part of the file name. Now when you use the {% static %} tag you'll see the file name with the hash.

If you're running nginx, update your site configuration so that browsers will cache static files for as long as possible:

location /static/ {
    expires max;

Then reload nginx.

Now you should be up and running with a good performance boost, and you won't have to ask users to refresh a page to get updated static files.

© Alex Tomkins.