Using cache_page Decorator

A snippet showing how to cache entire views with cache_page.


# views.py

from django.views.decorators.cache import cache_page
from django.http import HttpResponse

@cache_page(60 * 15)  # 15 minutes
def homepage(request):
    return HttpResponse("This page is cached.")
      
Explanation:
  • The entire homepage is cached for 15 minutes after the first request.
  • Significantly reduces database and template rendering load for subsequent requests.
  • Ideal for pages with content that doesn’t change frequently and have high traffic.
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.