Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resetting cache for Django cached template loader

Django 1.2 introduced a new template loader, that stores data in cache ( django.template.loaders.cached.Loader ).

Unfortunatly, i failed to find any info on how the cache is invalidated and when and how it is reset.

I want to use this on my server, but i'm not sure, that it would reset on django restart (that would be enough for me).

like image 443
DataGreed Avatar asked Nov 01 '10 17:11

DataGreed


2 Answers

from django.template.loader import template_source_loaders

def reset_template_cache():
    if not template_source_loaders:
        return

    for loader in template_source_loaders:
        loader.reset()

There you go :)

like image 187
Davide Callegari Avatar answered Oct 05 '22 04:10

Davide Callegari


The existing solutions written here broke around Django 1.9, due to the template loader system having been refactored. If you try to use the code that imports template_source_loaders and that import fails, try this:

from django.template.loader import engines

for engine in engines.all():
    engine.engine.template_loaders[0].reset()

This code assumes that your TEMPLATES setting is using the default 'loaders' option, or using only a single django.template.loaders.cached.Loader.

like image 25
coredumperror Avatar answered Oct 05 '22 04:10

coredumperror