Django TemplateView 在错误的地方搜索
Django TemplateView searching in wrong place
我的 Django 结构是:
testing
contacts
__init__.py
templates
contacts
index.html
(additional modules)
testing
__init__.py
templates
testing
test.html
urls.py
(additional modules)
在主 URL 模块中,testing.urls
,我有一个如下所示的 urlconf:
url(r'^testing/$', TemplateView.as_view(template_name='testing/test.html'))
问题是,它一直在 contacts.templates.contacts
中查找 test.html
文件。使用现有的 urlcon,调试页面显示如下:
模板加载程序事后分析
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/Library/Python/2.7/site-packages/django/contrib/admin/templates/testing/test.html (File does not exist)
/Library/Python/2.7/site-packages/django/contrib/auth/templates/testing/test.html (File does not exist)
/Users/*/Developer/django/testing/contacts/templates/testing/test.html (File does not exist)
它始终默认为......................................^^^^^^^^^^ contacts
文件夹出于某种原因。 TemplateView 或 template_name
是否有其他一些参数可以控制它?任何帮助表示赞赏!
更新 - 内部 settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
testing/testing
目录当前未被 Django 搜索。最简单的修复方法是将其添加到 DIRS
设置中:
'DIRS': [os.path.join(BASE_DIR, 'testing', 'templates')],
另一种选择是将 testing
添加到您的 INSTALLED_APPS
设置中。然后 Django 会找到您的模板,因为您有 APP_DIRS=True
。但是我不推荐这样做,因为在你的情况下 testing/testing
是包含 settings.py
和根 url 配置的特殊目录。
通过指定 "APP_DIRS": True
,您告诉 django 在每个已安装的应用程序中搜索模板文件。检查 settings.py
您的所有应用是否都包含在 INSTALLED_APPS
中。如果是,您可以尝试强制 Django 在您的应用程序中查找模板。
TEMPLATES = [
{
'DIRS': [os.path.join(BASE_DIR, 'testing', 'templates')],
....
},
]
我的 Django 结构是:
testing
contacts
__init__.py
templates
contacts
index.html
(additional modules)
testing
__init__.py
templates
testing
test.html
urls.py
(additional modules)
在主 URL 模块中,testing.urls
,我有一个如下所示的 urlconf:
url(r'^testing/$', TemplateView.as_view(template_name='testing/test.html'))
问题是,它一直在 contacts.templates.contacts
中查找 test.html
文件。使用现有的 urlcon,调试页面显示如下:
模板加载程序事后分析
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/Library/Python/2.7/site-packages/django/contrib/admin/templates/testing/test.html (File does not exist)
/Library/Python/2.7/site-packages/django/contrib/auth/templates/testing/test.html (File does not exist)
/Users/*/Developer/django/testing/contacts/templates/testing/test.html (File does not exist)
它始终默认为......................................^^^^^^^^^^ contacts
文件夹出于某种原因。 TemplateView 或 template_name
是否有其他一些参数可以控制它?任何帮助表示赞赏!
更新 - 内部 settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
testing/testing
目录当前未被 Django 搜索。最简单的修复方法是将其添加到 DIRS
设置中:
'DIRS': [os.path.join(BASE_DIR, 'testing', 'templates')],
另一种选择是将 testing
添加到您的 INSTALLED_APPS
设置中。然后 Django 会找到您的模板,因为您有 APP_DIRS=True
。但是我不推荐这样做,因为在你的情况下 testing/testing
是包含 settings.py
和根 url 配置的特殊目录。
通过指定 "APP_DIRS": True
,您告诉 django 在每个已安装的应用程序中搜索模板文件。检查 settings.py
您的所有应用是否都包含在 INSTALLED_APPS
中。如果是,您可以尝试强制 Django 在您的应用程序中查找模板。
TEMPLATES = [
{
'DIRS': [os.path.join(BASE_DIR, 'testing', 'templates')],
....
},
]