cache_page decorator

1 view
Skip to first unread message

Kevin

unread,
Nov 28, 2005, 5:29:43 PM11/28/05
to Django developers
I was trying to use the 2.4 syntax to add a cache decorator to my view
function as described in the documentation, eg:
@cache_page(60 * 15)
def slashdot_this(request):
...

But, the way that that
django.utils.decorators.decorator_from_middleware is implemented, it
doesn't seem to able to handle a decorator that takes arguments

Eg,

my code works if I write it as:
@cache_page
def slashdot_this(request):
.....

but not with the timeout argument.

It seems to be a result of having to add an extra layer of indirection
to decorators to support arguments, from the python docs:

Decorator functions can take arguments. If arguments are supplied, your
decorator function is called with only those arguments and must return
a new decorator function; this function must take a single function and
return a function, as previously described. In other words, @A @B
@C(args) becomes:

def f(): ...
_deco = C(args)
f = A(B(_deco(f)))

http://docs.python.org/whatsnew/node6.html

Can anyone else confirm this?

My code does work as follow:
slashdot_this = cache_page(slashdot_this, 60 * 15)

Reply all
Reply to author
Forward
0 new messages