Skip to main content

Django Fragment Cache

Django provides template fragment caching, such as :

{% load cache %}
{% cache 500 sidebar %}
    .. sidebar ..
{% endcache %}

But the variable sidebar still to be passed to the template every time, otherwise the fragment is blank when the cache expires.

If the data acquisition process is time consuming, then the fragment cache is to exist in name only.

So, this module provides another way to use the template cache.

How to use?

Install

pip install django-fragment-cache

Install Requires:

  • django >= 2

edit your settings.py to add fragment_cache to your NSTALLED_APPS.

INSTALLED_APPS = (
    ...
    'fragment_cache',
    ...
)

Use

you need provide a callable object to tell module how to get data, and the function return a dict.

only call function when cache expires.

import time
def get_data(num=10):
    time.sleep(3)
    return {'nums': list(range(num))}

def test(request):
    return render(request, 'test.html', {'get_data': get_data})

The {% fragment_cache %} template tag is similar to {% cache %} , for example:

{% load fragment_cache %}
{% fragment_cache 500 get_data %}
    <ul>
        {% for i in nums %}
            <li> {{ i }}</li>
        {% endfor %}
    </ul>
{% endfragment_cache %}

Sometimes, you might want to cache multiple copies of a fragment depending on some dynamic data that appears inside the fragment.

This allows different parameters to be cached to cache different results, for example:

{% load fragment_cache %}
{% fragment_cache 500 get_data 6 %}
    <ul>
        {% for i in nums %}
            <li> {{ i }}</li>
        {% endfor %}
    </ul>
{% endfragment_cache %}

Enjoy yourself.

Metadata

Release files for django-fragment-cache 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for django-fragment-cache 0.5.1
File Interpreter ABI Platform
django_fragment_cache-0.5.1-py3-none-any.whl Python 3 none any Details

Release files / django_fragment_cache-0.5.1-py3-none-any.whl

Download URL django_fragment_cache-0.5.1-py3-none-any.whl
Size 3.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
83dd438d617900929c65bfcfbe9fd436e8437129dc15ed23c0b9b7c31e3966bf
BLAKE2b-256 checksum
How to use checksums
d0e4da1fa1bd0c378f7afdce473e58b1a66d7380d6a40c5342077c50dc328671
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for django-fragment-cache 0.5.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.5.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page