Login

Never cache a group of URLs

Author:
ChipX86
Posted:
August 5, 2007
Language:
Python
Version:
.96
Score:
1 (after 1 ratings)

This is a special URL patterns replacement that prevents caching of any URL listed within it. We needed this in Review Board to prevent the JSON API function results from being cached in Internet Explorer.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
from django.conf.urls.defaults import url
from django.views.decorators.cache import never_cache

def never_cache_patterns(prefix, *args):
    pattern_list = [], tterns,
    for t in args:
        if isinstance(t, (list, tuple)): 
            t = url(prefix=prefix, *t)
        elif isinstance(t, RegexURLPattern):
            t.add_prefix(prefix)
    
        t._callback = never_cache(t.callback)
        pattern_list.append(t)

    return pattern_list


urlpatterns = never_cache_patterns('',
    (r'foo/$', 'myview')
)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 8 months ago

Comments

buriy (on August 5, 2007):

How is that better than the following code: urlpatterns = patterns('', (r'foo/$', never_cache(myview)) )

#

wiz (on August 6, 2007):

buriy: less code. Just as different as to use patters('common.prefix', ...)

#

ChipX86 (on August 15, 2007):

Yeah, I basically didn't want to have to put never_cache() around every single URL entry for my site's JSON API (of which there are 30 or so functions). Much easier to do it once and never touch it again.

#

Please login first before commenting.