How can you use the @cache_page decorator with Django class based views?

538 views
Skip to first unread message

daniels

unread,
Nov 14, 2015, 1:38:18 PM11/14/15
to Django users

The only method that seems to be working is adding the decorator in urls.py which is ugly.

Is there any way to apply this decorator in the view?

class HomeView(View):
    @method_decorator(cache_page(60 * 60))
    def dispatch(self, *args, **kwargs):
        return super(HomeView, self).dispatch(*args, **kwargs)

I've tried the above but it doesn't seem to be working. I've also tried adding it to the get method (which is what I want to cache).

I'm using FileBasedCache and only when I use cache_page decorator in urls.py I see files being created in my cache folder. With the method above nothing happens.

Simon Charette

unread,
Nov 14, 2015, 1:58:31 PM11/14/15
to Django users
Hi Daniels,

There's a tentative API being worked on Github. You might want to have a look at it.

Simon
Reply all
Reply to author
Forward
0 new messages