在错误的目录中查找模板
Looking for template in a wrong directory
我已将我的 Django 项目上传到 Openshift,python 代码工作正常,但是,我的模板是从错误的文件夹加载的:
/var/lib/openshift/55b9********************/templates
我的 settings.py 文件包含:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
正确指向
/var/lib/openshift/55b9********************/app-root/runtime/repo/wsgi/marko/templates
如 django 的回溯页面所示。为什么会这样?我可以将我的模板复制到它查找的文件夹中,但我不想将任何项目文件放在项目文件夹之外。 runserver
在本地计算机上查找正确文件夹中的模板。
你是 运行 Django 1.8 吗? TEMPLATE_DIRS
指令已弃用并由 TEMPLATES
取代。
根据 the docs,您应该像这样更新您的设置:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
# insert your TEMPLATE_DIRS here
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
# list if you haven't customized them:
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
假设 BASE_DIR/templates
指向正确的目录。
我已将我的 Django 项目上传到 Openshift,python 代码工作正常,但是,我的模板是从错误的文件夹加载的:
/var/lib/openshift/55b9********************/templates
我的 settings.py 文件包含:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
正确指向
/var/lib/openshift/55b9********************/app-root/runtime/repo/wsgi/marko/templates
如 django 的回溯页面所示。为什么会这样?我可以将我的模板复制到它查找的文件夹中,但我不想将任何项目文件放在项目文件夹之外。 runserver
在本地计算机上查找正确文件夹中的模板。
你是 运行 Django 1.8 吗? TEMPLATE_DIRS
指令已弃用并由 TEMPLATES
取代。
根据 the docs,您应该像这样更新您的设置:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
# insert your TEMPLATE_DIRS here
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
# list if you haven't customized them:
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
假设 BASE_DIR/templates
指向正确的目录。