Login

cache_page that does nothing

Author:
peterbe
Posted:
August 20, 2009
Language:
Python
Version:
1.1
Score:
0 (after 2 ratings)

When debugging/developing you want to be able to refresh your views every time you make a little change. But when in production mode you might want to cache these views because they contain long and resource hungry calculations or something.

By putting this above "hack" in after importing cache_page you only cache the views in production mode.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from django.conf import settings
from django.views.decorators.cache import cache_page

if settings.DEBUG:
    def cache_page(delay):
        def rendered(view):
            def inner(request, *args, **kwargs):
                return view(request, *args, **kwargs)
            return inner
        return rendered

@cache_page(60 * 60) # 1 hour
def my_view(request):
    ...expensive calculation...

More like this

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

Comments

larin (on August 21, 2009):

@gregb

+100 =)))

#

Please login first before commenting.