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.
- Category Performance & Optimization
- Total Views 318
- Last Modified 17 April, 2026
- Tags #performance #caching #views #decorator
Previous snippet
Database Indexing Example
Next snippet