Per-View Caching
The cache_page Decorator
cache_page wraps a view so that Django automatically stores its rendered response and serves that same response to future requests until the timeout expires, skipping the view logic entirely.
Note: Timeouts are given in seconds, so 60 * 15 means 15 minutes.
Example: The cache_page Decorator
cache_page wraps a view so that Django automatically stores its rendered response and serves that same response to future requests until the timeout expires, skipping the view logic entirely.
from django.views.decorators.cache import cache_page
@cache_page(60 * 15)
def article_list(request):
articles = Article.objects.all()
return render(request, 'articles/list.html', {'articles': articles})
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Caching Class-Based Views
For class-based views, cache_page can't be applied directly above the class. Instead, wrap the as_view() call in urls.py, or apply method_decorator to the view's dispatch method.
Example: Caching Class-Based Views
For class-based views, cache_page can't be applied directly above the class. Instead, wrap the as_view() call in urls.py, or apply method_decorator to the view's dispatch method.
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django.views.generic import ListView
@method_decorator(cache_page(60 * 15), name='dispatch')
class ArticleListView(ListView):
model = Article
template_name = 'articles/list.html'
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Per-View Caching in urls.py
Instead of decorating the view itself, you can wrap it where it's registered in urls.py. This keeps the view function clean and makes the caching behavior visible right next to the URL it applies to.
Example: Per-View Caching in urls.py
Instead of decorating the view itself, you can wrap it where it's registered in urls.py. This keeps the view function clean and makes the caching behavior visible right next to the URL it applies to.
from django.urls import path
from django.views.decorators.cache import cache_page
from . import views
urlpatterns = [
path('articles/', cache_page(60 * 15)(views.article_list), name='article-list'),
]
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Applying @cache_page to a view that shows different content per logged-in user, causing users to see someone else's cached page.
- Setting the cache timeout far longer than how often the underlying data actually changes.
- Forgetting that cached responses include the exact URL as part of the cache key, so query strings create separate cache entries.
- The @cache_page decorator caches an entire view's response for a set number of seconds.
- It's applied directly above a view function or wrapped around a class-based view's as_view().
- Per-view caching is coarse-grained -- it caches the whole HTML response, not just parts of it.
- It works best for public pages that look the same for every visitor.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: