Login

Snippets by jbrisbin

Snippet List

Cacheable resources

This snippet provides a template tag that automatically replaces references to any resource you want cached forever with a version of the file that is based on the MD5 sum. For an image, you would use something like: {% load utils %} <img src="{% cacheable "/media/images/logo.png" %}"/> To install it, put a setting in your settings.py file called "DOCUMENT_ROOT", put the python code into a templatetag-friendly file (e.g. app/templatetags/utils.py), load that template tag, then use either a string literal, as above, or a variable name to refer to your resource: <img src="{% cacheable my_media_file %}"/> The cacheable resource will be used when `DEBUG = False`, but in DEBUG mode, the path you give it will be passed back untouched (so you don't have a proliferation of cacheable files as you develop). Django will need write access to the directory you've specified as "DOCUMENT_ROOT" (so it can copy the original file into a forever-cacheable version). You'll also need to set up your webserver to serve files called "MYMD5SUMNAME.cache.(js|css|png|gif|jpg) with an expires header that is far into the future. The goal here is to create a version of your file that will never have to be downloaded again. If you ever change the original file, the MD5 sum will change and the changed file's cacheable name will reflect that. Besides simply changing the name of resources, if the file is a JavaScript or CSS file, and you've specified `MINIFY = True`, the file will be minified using YUI compressor.

  • md5
  • cacheable
  • cacheing
Read More

jbrisbin has posted 1 snippet.