| Author: | Filip Noetzel |
|---|---|
| Version: | v0.01 |
| Web: | http://j03.de/projects/django-hashedmedia/ |
| Git: | git clone http://j03.de/git/django-hashedmedia.git/ ( browse, also on github) |
| Download: | django-hashedmedia.tar.gz |
A django application, that makes Rule #3 ('Add a far future Expires header to your components') easy and efficient.
Say, your django webapp http://example.com/ references a CSS file http://static.example.com/main.css. A web browser loading the page for the first time (i.e. with an empty cache) will retrieve the whole file. The web browser will check at any subsequent page load, whether your css file has changed (and most of the time it has not changed).
Using django_hashedmedia, you refer to your assets (e.g. .js, .css, .png, etc.) by it's content's hash digest (40 byte SHA1): For example, your HTML will refer to blGX5jfkVqtFoZWlwlVia3l5BxI.css instead of main.css. You then configure your web server to serve your assets with an Expires:-header that is a few years in the far future. That way browsers need to retrieve your assets only once and after you change them.
Put the folder django_hashedmedia somewhere in your $PYTHONPATH (presumably your project folder, where your manage.py lives).
Then edit settings.py and add django_hashedmedia to the list of INSTALLED_APPS:
Replace your MEDIA_URL references with the tag {% hashed_media_url %}:
So,
would become
Note
Don't worry about using the {% load .. %} directive, the tag is added to every template automatically.
Open your settings.py and append the following lines (these are the default values, adapt to your needs):
For your production setup make sure your webserver serves the static media from HASHED_MEDIA_ROOT.
If you want to try if everything is working correctly with django_hashedmedia, set HASHEDMEDIA_ENABLED = True, if you want to develop with the speaking names of your asset files, set it to False.
Upon each deployment, run
See django-hashedfilestorage for a way to add far future expires headers also to your user-uploaded data.
django-hashedmedia is licensed as Beerware, patches and suggestions are welcome.