Hi. I thought I share some code with you.
All began with my laziness to set up a Apache or Lighttp server to serve up my static files for django. So I used djangos own django.views.static.serve to serve my files. Only problem was that I was unable to set Expires header for files served that way, so I made some modifications to django. Here's the result. Sample url.py file: from django.conf.urls.defaults import * from django.views.static import expires urlpatterns = patterns('', (r'^media/(.*)$', 'django.views.static.serve', { 'document_root': 'media', 'show_indexes': False, 'expires': expires((60 * 60, # default expiry time (r'\.jpg$', 60 * 60 * 7), (r'\.css$', 60 * 2), (r'\.js$', False), # uses default )) } ), ) and a patch to django: Index: django/views/static.py =================================================================== --- django/views/static.py (revision 5818) +++ django/views/static.py (working copy) @@ -9,7 +9,7 @@ import stat import urllib -def serve(request, path, document_root=None, show_indexes=False): +def serve(request, path, document_root=None, show_indexes=False, expires=False): """ Serve static files below a given point in the directory structure. @@ -55,6 +55,14 @@ contents = open(fullpath, 'rb').read() response = HttpResponse(contents, mimetype=mimetype) response["Last-Modified"] = rfc822.formatdate(statobj[stat.ST_MTIME]) + if expires: + from time import time + now = time() + for (path_re, path_expire, seconds) in expires: + if path_re.search(path): + response["Expires"] = rfc822.formatdate(now + seconds) + break + return response DEFAULT_DIRECTORY_INDEX_TEMPLATE = """ @@ -123,3 +131,22 @@ except (AttributeError, ValueError): return True return False + +def expires(expires_tuple): + """ + Precompiles regexes from expires list. + + expires_tuple + List of tuples containing path regex and number of seconds to add to current timestamp. + First item is a default seconds to add if path seconds is 0 or False. + + returns a new list with precompiled regexes + """ + expires_list = [] + default_seconds = expires_tuple[0] + for (path_re, seconds) in expires_tuple[1:]: + if not seconds: + seconds = default_seconds + expires_list.append((re.compile(path_re), path_re, seconds)) + return expires_list + -- Tõnis Kevvai --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-developers?hl=en -~----------~----~----~----~------~----~------~--~---