how to change the implicit default settings in django 1.6
-
21-12-2019 - |
Question
the 'implicit' I mean is that in Django 1.6 some settings are omitted in the generated settings.py (by django-admin startproject), for example, in the settings.py there won't be TEMPLATE_LOADERS placed there, but it actually has a default value:
$ ./manage.py shell
>>> from django.conf import settings
>>> print settings.TEMPLATE_LOADERS
('django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader')
I've tried with updating the settings.py like this:
TEMPLATE_LOADERS += (
'django.template.loaders.eggs.Loader',
)
but it will fail with the following error:
NameError: name 'TEMPLATE_LOADERS' is not defined
I'm just wondering whether there is a best practice to add additional template loaders to the default list without doing it like this (which a little ugly to repeat the default loaders):
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'django.template.loaders.eggs.Loader',
)
Solution
You are trying to update a non defined variable, you should create or import it. The default setting values are defined in django.conf.global_settings. If you add this line to the top of the file it should work:
from django.conf.global_settings import TEMPLATE_LOADERS
Not sure if it is a good practice either because you may want to know exactly which settings are you using.
The complete list of default settings is here:
django.conf.global_settings
And this is the mechanism Django uses to override default settings with user settings where mod are the user settings and global_settings are the default settings:
# django/conf/__init__.py
def __init__(self, settings_module):
# update this dict from global settings (but only for ALL_CAPS settings)
for setting in dir(global_settings):
if setting.isupper():
setattr(self, setting, getattr(global_settings, setting))
# store the settings module in case someone later cares
self.SETTINGS_MODULE = settings_module
try:
mod = importlib.import_module(self.SETTINGS_MODULE)
except ImportError as e:
raise ImportError(
"Could not import settings '%s' (Is it on sys.path? Is there an import error in the settings file?): %s"
% (self.SETTINGS_MODULE, e)
)
tuple_settings = ("INSTALLED_APPS", "TEMPLATE_DIRS")
self._explicit_settings = set()
for setting in dir(mod):
if setting.isupper():
setting_value = getattr(mod, setting)
if (setting in tuple_settings and
isinstance(setting_value, six.string_types)):
raise ImproperlyConfigured("The %s setting must be a tuple. "
"Please fix your settings." % setting)
setattr(self, setting, setting_value)
self._explicit_settings.add(setting)