Opened 9 years ago

Closed 8 years ago

Last modified 8 years ago

#25788 closed New feature (fixed)

Enable the cached template loader when DEBUG=False if no loaders are explicitly configured

Reported by: Jaap Roes Owned by: Harris Lapiroff
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently enabling the cached template loader is a lot of effort, you'll have to change your settings from something like this:

TEMPLATES = [{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    'APP_DIRS': True
}]

To this:

TEMPLATES = [{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    'OPTIONS': {
        'loaders': [
            ('django.template.loaders.cached.Loader', [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ]),
        ],
    },
}]

Making sure you don't forget to take out the APP_DIRS option, figuring out what loaders to use and getting the nesting of tuples and lists just right.

I propose adding an option the Django template engine called cache_templates to simplify all of this. Making the second example look more like this:

TEMPLATES = [{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    'APP_DIRS': True
    'OPTIONS': {'cache_templates': DEBUG}
}]

Change History (9)

comment:1 by Tim Graham, 9 years ago

I don't find it to be a lot of effort to copy/paste from the docs. On the other hand, it takes a non-trivial cognitive effort to maintain another option and teach users about what it does. I forget the justification for APP_DIRS (which seems slightly redundant) but I don't think adding more options like that is a good path to go down.

comment:2 by Tim Graham, 9 years ago

Triage Stage: UnreviewedSomeday/Maybe

comment:3 by Tim Graham, 9 years ago

Has patch: unset
Summary: Enabling the cached template loader should be easierEnable the cached template loader when DEBUG=False if no loaders are explicitly configured
Triage Stage: Someday/MaybeAccepted

Aymeric suggested to enable the cached template loader by default when DEBUG = False and no ‘loaders’ options is specified.

This is technically backwards-incompatible, but:

  • people who don’t usually configure it will get a free performance boost in production
  • people who do will be able to simplify their configurations

Currently many projects have a variant of this awful pattern:

# settings/prod.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'OPTIONS': {
            'loaders': [
                ('django.template.loaders.cached.Loader', [
                    'django.template.loaders.app_directories.Loader',
                ]),
            ],
        },
    },
]

# settings/dev.py

from .prod import *

TEMPLATES[0]['OPTIONS'].update({
    'loaders': [
        'django.template.loaders.app_directories.Loader',
    ],
})

His suggestion makes it possible to remove the configuration of ‘loaders’ and to rely only on DIRS and APP_DIRS instead.

comment:4 by Harris Lapiroff, 8 years ago

Owner: changed from nobody to Harris Lapiroff
Status: newassigned

comment:5 by Harris Lapiroff, 8 years ago

Has patch: set

comment:6 by Tim Graham, 8 years ago

Patch needs improvement: set

Some test failures need to be fixed.

comment:7 by Tim Graham, 8 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

Fixed test failures in a new PR.

comment:8 by GitHub <noreply@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 277fe2e8:

Fixed #25788 -- Enabled the cached template loader if debug is False.

comment:9 by Jaap Roes, 8 years ago

Thanks Harris/Tim! Just one question, how does this impact #25791?

Note: See TracTickets for help on using tickets.
Back to Top