Template fragment caching
If you're after even more control, you can also cache template fragments using the cache
template tag. To give your template access to this tag, put
{% load cache %}
near the top of your template. The {% cache %}
template tag caches the contents of the block for a given amount of time.
It takes at least two arguments: the cache timeout, in seconds, and the name to give the cache fragment. The name will be taken as is, do not use a variable.
For example:
{% load cache %} {% cache 500 sidebar %} .. sidebar .. {% endcache %}
Sometimes you might want to cache multiple copies of a fragment depending on some dynamic data that appears inside the fragment.
For example, you might want a separate cached copy of the sidebar used in ...
Get Mastering Django: Core now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.