← Back to Django Course | Chapter 15: Security, Caching & Deployment | Lesson 2 of 9

Per-View Caching

Per-view caching is like photocopying a finished worksheet so the teacher can hand out the same copy instead of rewriting it by hand every time.

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.

markup
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.

markup
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.

markup
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. #}
Common Mistakes
  1. Applying @cache_page to a view that shows different content per logged-in user, causing users to see someone else's cached page.
  2. Setting the cache timeout far longer than how often the underlying data actually changes.
  3. Forgetting that cached responses include the exact URL as part of the cache key, so query strings create separate cache entries.
Chapter Summary
  • 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.