3

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.

4 Answers 4

5

You can use the method_decorator to decorate the view's dispatch() method, according to the docs. The most succinct way is like this:

from django.utils.decorators import method_decorator
from django.views.generic import ListView

@method_decorator(cache_page(60*60), name='dispatch')
class MyListView(ListView):
    # Your view code here.
Sign up to request clarification or add additional context in comments.

Comments

2

use it in urls.py

url(r'^home_url/?$', cache_page(60*60)(HomeView.as_view())),

1 Comment

I'd prefer to not use urls.py as I said in my first line "The only method that's seems to be working is adding the decorator in urls.py which is ugly."
1

The method_decorator approach should work just fine (we are using it in our project). Are you sure this is the problem?

this is the code we are using (with names changed) - seems like yours

class MyClassView(CommonMixin, View):
    @method_decorator(cache_page(60*60, cache='cache1'))
    def get(self, request):
                .....

4 Comments

Well when I add the decorator in urls.py I see files being created in my cache folder (that I defined in settings.py) but when I use the method_decorator as in my example above no file is created so I assume it does not work. (I did restarted the dev server after every change that I made in the code)
Is there any way you can provide a snippet that works for you?
@daniels it seems like your code, but I've added it in the answer with names changed.
Still doesn't work... No idea what the issue could be... I also don't get any error or anything in the console. It's just that the files are not created in the cache folder while if I do this in urls.py they are. Using latest version of Django 1.8.6
1

Let us say we want to cache the result of the myView view -

from django.views.decorators.cache import cache_page
@cache_page(60 * 15)
def myView(request, year, month):
    text = "Displaying articles of : %s/%s"%(year, month)
    return HttpResponse(text)

and urls.py will be

urlpatterns = patterns('myapp.views',url(r'^articles/(?P<month>\d{2})/(?P<year>\d{4})/', 'myView', name = 'articles'),)

2 Comments

How can we cache the queryset used in the view by the use of this decorator ? I am failing to do so. If it cache the result of this view, what will be the key under which it is stored ? Please answer both the questions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.