---
title: '{% cache %}'
description: caches part of a template.
date: '2026-08-21'
categories:
  - Template Tag
  - Utility
canonical: https://www.djangotemplatetagsandfilters.com/tags/cache/
doc_link: https://docs.djangoproject.com/en/6.1/topics/cache/#std-templatetag-cache
---

# {% cache %}

caches part of a template.

## Documentation

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

### Template

```django
{% 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.
