[Django]-Django HTML Template Cache

3๐Ÿ‘

You cache fragments of a template using the {% cache %} template tag:

{% load cache %} <-- at the top of the template

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

It takes at least two arguments: the cache timeout, in seconds, and
the name to give the cache fragment.

๐Ÿ‘คdoru

3๐Ÿ‘

TD;DR

HTML templates are compiled and cached in memory. Modifying the original HTML template files will not purge existing cached templates. One way to reset the cache is to restart the uwsgi/gunicorn service.

How template cache works?

Considring the following Django settings (https://docs.djangoproject.com/en/2.2/ref/settings/), OPTIONS['loaders'] isnโ€™t specified and OPTIONS['debug'] is False (defaults to the value of DEBUG).

DEBUG = False

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

django.template.loaders.cached.Loader is then automatically enabled. The cached loader then stores the compiled Template in memory after unknown templates are first encountered. These compiled templates are stored in a dict named get_template_cache (though the variable name sounds like a method).

๐Ÿ‘คft9

0๐Ÿ‘

for css and js files you can use django-compressor that can compress files and every time developer change js or css codes, just run manage.py compress on server and automatic generate new files that has uuid at end of files name and remove last files

for other part for other parts of page do as doru said

๐Ÿ‘คhn_tired

Leave a comment