{% cache %} Tag

caches part of a template.

Utility Most Useful As Markdown

Argument(s)
expiry_seconds fragment_name [vary_on…]
Closing tag
Required — {% endcache %}
Requires
{% load cache %}

Documentation

Caches whatever is between the tags, so an expensive fragment is rendered once rather than on every request.

Template

{% load cache %}
{% cache 500 sidebar request.user.username %}
  {% include "expensive_sidebar.html" %}
{% endcache %}

The first argument is how many seconds to keep it, the second names the fragment, and anything after that varies the key. Forgetting to vary on the thing that makes the fragment different — the user, the language, the object — is how one visitor ends up seeing another visitor's sidebar.

More Utility Tags

All Utility Tags


Did we get something wrong? Is there a use case for the cache tag that we should add? Please let us know.

Send Feedback

Official Documentation
This page last updated on August 20, 2026