'隐含'我的意思是在Django 1.6中,在生成的settings.py(由django-admin startproject)中省略了一些设置,例如,在settings.py中不会放置TEMPLATE_LOADERS,但是它实际上有一个默认值:
$ ./manage.py shell
>>> from django.conf import settings
>>> print settings.TEMPLATE_LOADERS
('django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader')
我尝试过像这样更新settings.py:
TEMPLATE_LOADERS += (
'django.template.loaders.eggs.Loader',
)
但它会因以下错误而失败:
NameError: name 'TEMPLATE_LOADERS' is not defined
我只是想知道是否有一个最佳做法是将其他模板加载器添加到默认列表而不像这样(重复默认加载器有点难看):
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'django.template.loaders.eggs.Loader',
)
答案 0 :(得分:2)
您正在尝试更新未定义的变量,您应该创建或导入它。默认设置值在django.conf.global_settings中定义。 如果您将此行添加到文件的顶部,它应该可以工作:
from django.conf.global_settings import TEMPLATE_LOADERS
不确定这是不是一个好习惯,因为您可能想知道您正在使用哪些设置。
默认设置的完整列表如下:
django.conf.global_settings
这是Django使用用户设置覆盖默认设置的机制,其中mod是用户设置,global_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)