Template Fragment Caching
Cache just the expensive parts of a page.
Template Fragment Caching is a free Django Academy lesson on CoddyKit — lesson 3 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 Just One Piece
Often only part of a page is slow. Fragment caching lets you cache that one block while the rest renders fresh each time.
Load the cache Tag
Fragment caching lives in a template tag library, so start your template with load cache to make it available.
{% load cache %}The cache Block
Wrap the slow markup in a cache block. Django renders it once, stores the HTML, and reuses it on later requests.
{% cache 500 sidebar %}
...expensive sidebar...
{% endcache %}Timeout Comes First
The first argument is the timeout in seconds. After it expires, the next render rebuilds the fragment and caches it again.
Name Your Fragment
The second argument is a fragment name. Django combines it with extra arguments to form the unique cache key.
Vary the Fragment
Add variables after the name so a fragment can vary per user or object, caching one copy for each distinct value.
{% cache 500 sidebar request.user.id %}
...per-user sidebar...
{% endcache %}Each Variation Is Separate
Every combination of the vary arguments gets its own key, so user 1 and user 2 each see their own cached copy.
Use a Specific Cache
Point a fragment at a named cache with using when you keep page fragments separate from other cached data.
{% cache 500 sidebar using="fragments" %}Great for Shared Widgets
Fragment caching shines for shared widgets like a navbar or a top-posts list that look the same for many visitors.
Keep Live Parts Outside
Leave anything that must stay current, like a greeting with the user's name, outside the cache block so it never goes stale.
Less Effort Than View Caching
Fragment caching is the middle ground: more precise than caching a whole view, yet far simpler than caching every query by hand.
Quick Check
Let's check how a fragment stays unique per user.
Recap
You can now load cache, wrap a fragment in a cache block with a timeout and name, and vary it per user for precise speedups. ✨
Frequently asked questions
Is the “Template Fragment Caching” lesson free?
Yes — the full text of “Template Fragment 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 “Template Fragment Caching”?
Cache just the expensive parts of a page. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Template Fragment 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
- Cache Backends and Redis
- Per-View and Per-Site Caching
- Template Fragment Caching
- Low-Level cache API and Invalidation