cache_page with Class Based Views

前端 未结 8 1964
孤独总比滥情好
孤独总比滥情好 2020-12-14 06:23

I\'m trying to do cache_page with class based views (TemplateView) and i\'m not able to. I followed instructions here:

Django--URL Caching Failing for Class Based Vi

8条回答
  •  我在风中等你
    2020-12-14 07:13

    You can simply decorate the class itself instead of overriding the dispatch method or using a mixin.

    For example

    from django.views.decorators.cache import cache_page
    from django.utils.decorators import method_decorator
    
    @method_decorator(cache_page(60 * 5), name='dispatch')
    class ListView(ListView):
    ...
    

    Django docs on decorating a method within a class based view.

提交回复
热议问题