#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 , 10 years ago
comment:3 by , 10 years ago
| Has patch: | unset |
|---|---|
| Summary: | Enabling the cached template loader should be easier → Enable the cached template loader when DEBUG=False if no loaders are explicitly configured |
| Triage Stage: | Someday/Maybe → Accepted |
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 , 9 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:7 by , 9 years ago
| Patch needs improvement: | unset |
|---|---|
| Triage Stage: | Accepted → Ready for checkin |
Fixed test failures in a new PR.
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.