1 2 3 4 5 6 7 8 9 10 11 12 13 | import os
from django.conf import settings
from django import template
register = template.Library()
@register.simple_tag
def versioned_media(path):
"""Allows auto versioning of files based on modification times.
Example: {% versioned_media "script.js" %} returns '/site_media/script.js?1217877755'
"""
modification_time = os.path.getmtime(os.path.join(settings.MEDIA_ROOT, path))
return "".join([settings.MEDIA_URL, path, "?%s" % int(modification_time)])
|
More like this
- Use the WSGIAccessScript Directive to secure static files based on the Django session by LuckiDog 2 years, 1 month ago
- ____ by matterk 5 years, 2 months ago
- Apache X-sendfile with permissions checking by h0axify 1 year, 2 months ago
- Custom mod_python AuthenHandler by aeby 5 years, 10 months ago
- Middleware to move tags <script> to the bottom by marinho 2 years, 11 months ago
Comments
There are two issues:
AFAIK using query string parameter may cause some browsers not to cache scripts
there already is nice solution for this kind of stuff: django-compress
I had my own solution, similiar to yours, but when django-compress appeared I've switched to it and so far I'm very happy with this.
#
I used django_templatecomponents which provides most of the minification and grouping features django-compress does.
In regards to the query string causing some browsers not to cache scripts, can you elaborate?
For my applications I don't really need to support older browsers.
#
Ok, so I found http://www.mnot.net/blog/2006/05/11/browser_caching
Works for:
And above
However, with Safari and Opera, only if the expires headers are present.
#