Template Fragment Caching
A snippet showing how to use template fragment caching in Django.
<!-- templates/home.html -->
{% load cache %}
{% cache 600 sidebar user.id %}
<div class="card">
<div class="card-header">Sidebar</div>
<div class="card-body">
{% include "partials/sidebar_links.html" %}
</div>
</div>
{% endcache %}
Explanation:
- Keys can include multiple arguments-great for per-language or per-user fragments.
- The sidebar block is cached for 10 minutes per-user (keyed by user.id).
- Category Performance & Optimization
- Total Views 519
- Last Modified 13 April, 2026
- Tags #performance #caching #templates #optimization
Previous snippet
Redis Cache Setup
Next snippet