Login

Django & cache headers

Author:
ludvig.ericson
Posted:
July 25, 2007
Language:
Python
Version:
.96
Score:
4 (after 4 ratings)

This takes advantage of a recently added feature to django, being able to give it real functions as the view instead of having it be a string that is has to look up itself.

It takes advantage of how decorators work and how cache_control works, normally you'd do something like this:

@cache_control(private=True, public=False)
def view_stuff(request):
    # ...
    return response

Which is equal to doing view_stuff = cache_control(private=True, public=False)(view_stuff) after definition. cache_control is a function factory, more or less, which we use to our advantage.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from django.views.decorators.cache import cache_control
from someproj.someapp.views import foo_view

# ...

open_cache = cache_control(private=True, public=True)

urlpatterns = patterns('',
    (r'^foo/$', open_cache(foo_view)),
    # ...
)

More like this

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

Comments

Please login first before commenting.