0Pricing
Django Academy · Lesson

Per-View and Per-Site Caching

Cache whole pages with decorators.

Per-View and Per-Site Caching is a free Django Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Cache the Whole Page

Sometimes a full page is expensive to build. Django can store the entire rendered response and replay it for the next visitor.

The cache_page Decorator

Wrap a view in cache_page and Django caches its full output for the number of seconds you pass in.

from django.views.decorators.cache import cache_page

@cache_page(60 * 15)
def article_list(request):
    ...

What Per-View Caching Stores

Per-view caching keeps the HttpResponse for that view only, so just the pages you decorate get this speedup.

Caching in the URLconf

You can also apply cache_page in urls.py without touching the view, useful for views you do not own.

path("news/", cache_page(60)(news_view))

Per-Site Caching

To cache nearly every page at once, Django offers per-site caching driven by two middleware classes instead of decorators.

The Two Middleware

Add UpdateCacheMiddleware at the top of the list and FetchFromCacheMiddleware at the bottom for per-site caching to work.

MIDDLEWARE = [
    "django.middleware.cache.UpdateCacheMiddleware",
    "...your middleware...",
    "django.middleware.cache.FetchFromCacheMiddleware",
]

The Site Cache Seconds

Set CACHE_MIDDLEWARE_SECONDS to control how long every cached page stays fresh across the whole site.

CACHE_MIDDLEWARE_SECONDS = 600

Only GET and HEAD

Both strategies cache only safe GET and HEAD requests, so form submissions and other POSTs are never served from cache.

Never Cache Private Pages

Skip caching on user-specific pages with never_cache, since a dashboard must never leak another person's data.

from django.views.decorators.cache import never_cache

@never_cache
def dashboard(request):
    ...

Vary Headers Matter

Use vary_on_headers so a cached page can differ by request details like language or device, keeping the right copy per visitor.

Pick the Right Scope

Reach for per-view caching when only a few heavy pages need help, and per-site when almost everything is read-only.

Quick Check

Let's nail down how Django decides what to cache.

Recap

You learned cache_page for single views and the cache middleware for the whole site, plus never_cache to protect private pages. 🚀

Frequently asked questions

Is the “Per-View and Per-Site Caching” lesson free?

Yes — the full text of “Per-View and Per-Site Caching” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.

What will I learn in “Per-View and Per-Site Caching”?

Cache whole pages with decorators. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Django Academy?

No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Per-View and Per-Site Caching” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Django Academy lesson?

Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Cache Backends and Redis
  2. Per-View and Per-Site Caching
  3. Template Fragment Caching
  4. Low-Level cache API and Invalidation
← Back to Django Academy