It’s considered a good practice to enable template caching in Django. It 
saves on the overhead of loading and parsing templates and turning them 
into template objects, which can give a nice performance boost when 
repeatedly rendering the same templates.

Currently it’s possible to enable template caching by using the cached 
loader. To do so you’ll need to add the loaders option to the engine 
settings and set it to a nesting of lists/tuples (I’ll wager no one can get 
this correct on the first try without first consulting the docs). This 
makes enabling template caching needlessly hard, and the discoverability of 
this feature lower than necessary. To this day I run into developers who 
have worked with Django for years but are unaware of the cached loader.

One of the downsides of enabling the cached loader is that it will cache 
templates until the server is reloaded. This makes it ill suited for 
development. I have, on many occasions, seen developers (including myself) 
forget to turn off template caching in their local settings, resulting in 
lost time trying to figure out why their changes are not showing.

I think Django can, and should do better. The bare minimum is to make 
template caching a simple toggle option that can be passed to the engine 
(https://code.djangoproject.com/ticket/25788 / 
https://github.com/jaap3/django/commit/21e9d3aa0eb0a9d2728f8a35318d49d528f3a60f 
(an admittedly naive patch)). This way cache_templates could simply mirror 
DEBUG and (new) projects can have a sane template caching configuration by 
default.

It would also be useful to have caching enabled in development so the 
template loading behaviour is (mostly) the same in development as it is in 
production.

One reason for this is that custom template tags need to be thread safe 
(https://docs.djangoproject.com/en/1.8/howto/custom-template-tags/#thread-safety-considerations).
 
Because cached templates always reuse the nodes it’s easier to notice when 
a template tag misbehaves.

Another reason is that the performance of template rendering will be 
roughly the same development as in production, which can make a great 
difference if you’re working with complex templates and/or custom tags that 
perform expensive operations in their __init__.

Off course, having caching on in development is impractical if templates 
stay cached forever. So some sort of template invalidation and reloading 
system needs to be added. It’s actually fairly easy to add this to the 
cached loader https://code.djangoproject.com/ticket/25791 / 
https://github.com/jaap3/django/commit/20e1caa9f923d82ce60dff155304de5289242186 
(an earlier/alternative implementation that moves caching to the engine can 
be found here: 
https://github.com/prestontimmons/django/commit/4683300c60033ba0db472c81244f01ff932c6fb3).

With caching and auto reloading as a core feature of Django’s engine it 
will be (somewhat) on par with the alternative Jinja2 engine shipped by 
Django. Jinja2’s Engine lets you configure an Environment 
(http://jinja.pocoo.org/docs/dev/api/#jinja2.Environment) using the 
engine's options. This includes setting a bytecode_cache and enabling 
auto_reload (this last option mirrors DEBUG by default in Django).

So in short, I propose making template caching a true feature of Django’s 
template engine. Which can be configured in a way similarl to the Jinja2 
engine (and could be on by default for new projects):

TEMPLATES = [{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    'APP_DIRS': True
    'OPTIONS': {
        'cache_templates': True, 
        'auto_reload': DEBUG  // this could be the default set by the engine
    }
}]

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/c2897e2b-c506-44e3-93e4-cb077bb8f4ac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to