Django 模板不存在?
- 2025-02-07 08:44:00
- admin 原创
- 63
问题描述:
我的本地机器在 Ubuntu 8.10 上运行 Python 2.5 和 Nginx,并使用从最新开发主干构建的 Django。
对于我请求的每个 URL,它都会抛出:
TemplateDoesNotExist 位于 /appname/path appname/template_name.html
Django 尝试按以下顺序加载这些模板: 使用加载器 django.template.loaders.filesystem.function: 使用加载器 django.template.loaders.app_directories.function:
TEMPLATE_DIRS ('/usr/lib/python2.5/site-packages/projectname/templates',)
在这种情况下,它是否在寻找/usr/lib/python2.5/site-packages/projectname/templates/appname/template_name.html?奇怪的是,这个文件确实存在于磁盘上。为什么 Django 找不到它?
我在 Ubuntu 9.04 上使用 Python 2.6 在远程服务器上运行相同的应用程序,没有出现这样的问题。其他设置相同。
我的本地机器上是否存在配置错误,或者可能导致此类错误的原因是什么,我需要调查吗?
在我的settings.py中,我已指定:
SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))
# Find templates in the same folder as settings.py.
TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, 'templates'),
)
它应该寻找以下文件:
/usr/lib/python2.5/site-packages/projectname/templates/appname1/template1.html
/usr/lib/python2.5/site-packages/projectname/templates/appname1/template2.html
/usr/lib/python2.5/site-packages/projectname/templates/appname2/template3.html
...
所有上述文件均存在于磁盘上。
已解决
我试过后它现在可以工作了:
chown -R www-data:www-data /usr/lib/python2.5/site-packages/projectname/*
这很奇怪。我不需要在远程服务器上执行此操作即可使其工作。
解决方案 1:
第一个解决方案:
这些设置
TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, 'templates'),
)
templates/
意味着 Django 将从项目下的目录中查看模板。
假设你的 Django 项目位于/usr/lib/python2.5/site-packages/projectname/
,那么根据你的设置,django 将查找下面的模板/usr/lib/python2.5/site-packages/projectname/templates/
因此在这种情况下,我们希望将模板结构调整如下:
/usr/lib/python2.5/site-packages/projectname/templates/template1.html
/usr/lib/python2.5/site-packages/projectname/templates/template2.html
/usr/lib/python2.5/site-packages/projectname/templates/template3.html
第二种解决方案:
如果这仍然不起作用并假设您已在 settings.py 中配置应用程序,如下所示:
INSTALLED_APPS = (
'appname1',
'appname2',
'appname3',
)
默认情况下,Django 将加载每个已安装应用程序下的目录下的模板templates/
。因此,对于您的目录结构,我们希望将模板移动到如下位置:
/usr/lib/python2.5/site-packages/projectname/appname1/templates/template1.html
/usr/lib/python2.5/site-packages/projectname/appname2/templates/template2.html
/usr/lib/python2.5/site-packages/projectname/appname3/templates/template3.html
SETTINGS_PATH
默认情况下可能未定义。在这种情况下,您需要定义它(在 settings.py 中):
import os
SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__))
解决方案 2:
查找此元组:
import os
SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__))
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',
],
},
},
]
您需要将字符串添加到“DIRS”
os.path.join(BASE_DIR, 'templates')
因此,您总共需要:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(SETTINGS_PATH, 'templates')],
'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',
],
},
},
]
解决方案 3:
如果您在从头添加时遇到此问题app
。 可能是因为您错过了一些settings
。 添加时需要三个步骤app
。
1、创建目录和模板文件。
假设您有一个名为的项目mysite
,并且想要添加一个app
名为的your_app_name
。将您的模板文件放在mysite/your_app_name/templates/your_app_name
以下位置。
├── mysite
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── your_app_name
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── templates
│ │ └── your_app_name
│ │ └── my_index.html
│ ├── urls.py
│ └── views.py
2、将您的添加app
到INSTALLED_APPS
。
调整settings.py
INSTALLED_APPS = [
...
'your_app_name',
...
]
3、将您的app
目录添加到DIRS
中TEMPLATES
。
调整settings.py
。
添加操作系统导入
import os
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, 'your_app_name', 'templates', 'your_app_name'),
...
]
}
]
解决方案 4:
在设置.py 中删除TEMPLATE_LOADERS 和 TEMPLATE DIRS 然后添加
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['/home/jay/apijay/templates',],
'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',
],
},
},
]
解决方案 5:
我遇到了一个令人尴尬的问题...
我收到此错误是因为我太匆忙,忘记将应用程序放入INSTALLED_APPS
。您可能会认为 Django 会引发更具描述性的错误。
解决方案 6:
2023 年 5 月更新:
templates
文件夹可以放在django-project
文件夹正下方或每个文件夹正下方,app
如下所示:
django-project
|-core
│ |-settings.py
│ └-urls.py
|-app1
| |-urls.py
| └-views.py
|-app2
| |-urls.py
| └-views.py
└-templates # Here
|-app1
| └-a1.html
└-app2
└-a2.html
或者:
django-project
|-core
│ |-settings.py
│ └-urls.py
|-app1
| |-urls.py
| |-views.py
| └-templates # Here
| └-app1
| └-a1.html
└-app2
|-urls.py
|-views.py
└-templates # Here
└-app2
└-a2.html
例如首先,您需要设置"app1"
INSTALLED_APPS "app2"
,如下所示settings.py
:
# "core/settings.py"
INSTALLED_APPS = [
...
"app1",
"app2",
]
然后,如果templates
文件夹就在django-project
文件夹下方,如下所示:
django-project
|-core
│ |-settings.py
│ └-urls.py
|-app1
| |-urls.py
| └-views.py
|-app2
| |-urls.py
| └-views.py
└-templates # Here
|-app1
| └-a1.html
└-app2
└-a2.html
然后,您需要在TEMPLATESBASE_DIR / 'templates'
中将其设置为“DIRS”,如下所示。 *我建议将文件夹放在如上所示的文件夹下方,因为您可以轻松地在一个地方管理模板:settings.py
`templates`django-project
# "core/settings.py"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [
BASE_DIR / 'templates' # Here
],
"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",
],
},
},
]
并且,如果templates
文件夹就位于每个app
文件夹下方,如下所示:
django-project
|-core
│ |-settings.py
│ └-urls.py
|-app1
| |-urls.py
| |-views.py
| └-templates # Here
| └-app1
| └-a1.html
└-app2
|-urls.py
|-views.py
└-templates # Here
└-app2
└-a2.html
然后,您需要保持为"DIRS"
空(这是默认值),而不设置为BASE_DIR / 'templates'
,"DIRS"
如下所示:TEMPLATES
`settings.py`
# "core/settings.py"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [], # Keep it empty
"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",
],
},
},
]
test()
然后,在和中定义app1/views.py
,app2/views.py
如下所示。请注意,您需要设置"app1/a1.html"
和,"app2/a2.html"
而不仅仅是在render()"a1.html"
中设置和,如下所示:"a2.html"
# "app1/views.py"
from django.shortcuts import render
def test(request): # Don't set just "a1.html"
return render(request, "app1/a1.html")
# "app2/views.py"
from django.shortcuts import render
def test(request): # Don't set just "a2.html"
return render(request, "app2/a2.html")
然后,在和中设置和test
的各个视图,如下所示:app1
`app2app1/urls.py
app2/urls.py`
# "app1/urls.py"
from django.urls import include, path
from . import views
app_name = "app1"
urlpatterns = [
path("", views.test, name='test'), # Here
]
# "app2/urls.py"
from django.urls import include, path
from . import views
app_name = "app2"
urlpatterns = [
path("", views.test, name='test'), # Here
]
然后,如下所示设置和中urls.py
的每个,然后 和 的模板将被呈现而不会出现任何错误:app1
`app2core/urls.py
app1`app2
# "core/urls.py"
from django.urls import include, path
urlpatterns = [
...
path("app1/", include('app1.urls')), # Here
path("app2/", include('app2.urls')) # Here
]
最后,我建议将templates
文件夹放在文件夹下方django-project
,如下所示,因为您可以轻松地在一个地方管理模板:
django-project
|-core
│ |-settings.py
│ └-urls.py
|-app1
| |-urls.py
| └-views.py
|-app2
| |-urls.py
| └-views.py
└-templates # Here
|-app1
| └-a1.html
└-app2
└-a2.html
解决方案 7:
从版本 上测试的 Django 版本开始3
,您需要将新应用添加到已安装的应用中。Django 应用无需进行其他代码更改
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'addyourappnamehere'
]
解决方案 8:
对于 django 版本 1.9,我添加了
'DIRS': [os.path.join(BASE_DIR, 'templates')],
在 settings.py 中的 Templates 块中添加一行,效果很好
解决方案 9:
DjangoTemplateDoesNotExist
错误简单地意味着框架找不到模板文件。
要使用模板加载 API,您需要告诉框架您存储模板的位置。settings.py
执行此操作的位置是设置文件 ()中的TEMPLATE_DIRS
设置。默认情况下,它是一个空元组,因此此设置会告诉 Django 的模板加载机制在哪里查找模板。
选择您想要存储模板的目录并将其添加到 TEMPLATE_DIRS 例如:
TEMPLATE_DIRS = (
'/home/django/myproject/templates',
)
解决方案 10:
只是一种预感,但请查看有关 Django 模板加载的这篇文章。特别是,确保您django.template.loaders.app_directories.Loader
的 TEMPLATE_LOADERS 列表中有。
解决方案 11:
检查模板和 appname 目录的权限,使用 ls -l 或尝试从 django 执行绝对路径 open()。
解决方案 12:
我试过后它现在起作用了
chown -R www-data:www-data /usr/lib/python2.5/site-packages/projectname/*
这很奇怪。我不需要在远程服务器上执行此操作即可使其工作。
另外,我必须在本地机器上运行以下命令才能使所有静态文件可访问,但在远程服务器上它们都是“root:root”。
chown -R www-data:www-data /var/www/projectname/*
本地机器运行的是 Ubuntu 8.04 桌面版。远程服务器运行的是 Ubuntu 9.04 服务器版。
有人知道为什么吗?
解决方案 13:
确保您已将您的应用添加到project-name/app-namme/settings.py
INSTALLED_APPS:。
INSTALLED_APPS = ['app-name.apps.AppNameConfig']
关于 project-name/app-namme/settings.py
模板:。
'DIRS': [os.path.join(BASE_DIR, 'templates')],
解决方案 14:
我必须使用内部 APP 的模板,它对我有用:
'DIRS': [os.path.join(BASE_DIR + '/THE_APP_NAME', 'templates')],
解决方案 15:
如果是 django rest 框架,则添加rest_framework
到 INSTALLED_APPS。就我的情况而言,我忘记将其添加到已安装的应用程序中。
INSTALLED_APPS = [
'..........',,
'rest_framework',
'.........',
]
解决方案 16:
查看 django 尝试加载模板的文件夹,并Template-loader postmortem
在错误页面中查看,例如,错误将是这样的:
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: d:projectscsrccsrc emplatesase.html (Source does not exist)
我的错误vcsrccsrc emplatesase.html
是路径不对。
然后将文件更改TEMPLATES
为setting.py
模板路径
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# 'DIRS': [],
'DIRS': [os.path.join(BASE_DIR, 'vcsrc/templates')],
...
解决方案 17:
在您的setting.py
文件中用此替换DIRS
数组TEMPLATES
'DIRS': []
对此
'DIRS': [os.path.join(BASE_DIR, 'templates')],
但我认为你需要知道的是,你必须创建一个有名称的文件夹templates
,并且它应该在根路径上,否则你必须更改该DIRS
值
解决方案 18:
就我而言,只需将我的应用程序包含在settings.py文件中的INSTALLED_APPS中就足够了:
INSTALLED_APPS = [
"myapp",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
]
另外,请记住模板应该像这样放在您的目录中:myapp/templates/myapp/template_name.html
但是当您指向此模板时,您可以这样做:template = loader.get_template("myapp/template_name.html")
解决方案 19:
django配置为在使用 render(request, 'app_name/template.html', context) 引用时使用 project_name/app_name/templates/app_name/template.html 中的模板
如果您收到模板异常,原因是您没有在设置中将 app_name 添加到 mounted_apps 中。
我遇到了 django 4.1 的问题
解决方案 20:
检查您在项目文件夹下创建的应用程序是否已添加到INSTALLED_APPS中。就我的情况而言,我忘记将其添加到已安装的应用程序中。
例如,如果应用程序创建的名称是 hello_world,
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'hello_world', # <-- ADD THIS LINE
]
解决方案 21:
检查你的 templates.html 是否在/usr/lib/python2.5/site-packages/projectname/templates
目录中。
解决方案 22:
大家好,我找到了一个新的解决方案。实际上它是在另一个模板中定义的,因此您无需自己定义 TEMPLATE_DIRS,只需将目录路径名放在其下即可:
解决方案 23:
我很不好意思承认这一点,但对我来说问题在于模板被指定为….hml
而不是….html
。小心!
解决方案 24:
我添加了这个
TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, 'templates'),
)
但它仍然显示错误,然后我意识到在另一个项目中,模板显示时没有在 settings.py 文件中添加该代码,所以我检查了那个项目,我意识到我没有在这个项目中创建虚拟环境,所以我做了
virtualenv env
并且成功了,不知道为什么
解决方案 25:
我想到了这个问题。下面是我解决这个问题的方法:
查看您的 settings.py,找到TEMPLATES
变量,在 TEMPLATES 中,在DIRS
列表中添加您的模板路径。对我来说,首先我将模板路径设置为TEMPLATES_PATH = os.path.join(BASE_DIR,'templates')
,然后添加TEMPLATES_PATH
到DIRS
列表中。'DIRS':[TEMPLATES_PATH,]
然后重新启动服务器,TemplateDoesNotExist 异常就消失了。就是这样。
解决方案 26:
1.在您的“应用程序”中创建一个文件夹“templates”(假设您将其命名为应用程序),您可以将 html 文件放在这里。但强烈建议在“templates”文件夹中创建一个同名文件夹(“app”),然后将 html 文件放在那里。放入“app/templates/app”文件夹
2.现在在'app'的urls.py中输入:
path('', views.index, name='index'), # in case of use default server index.html
在'app'的views.py中输入:
from django.shortcuts import render
def index(request): return
render(request,"app/index.html")
# name 'index' as you want
解决方案 27:
适用于 Django 3
我发现我相信好方法,我在根文件夹中有 base.html,并且所有其他 html 文件都在 App 文件夹中,我 settings.py
import os
# This settings are to allow store templates,static and media files in root folder
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')
STATIC_DIR = os.path.join(BASE_DIR,'static')
MEDIA_DIR = os.path.join(BASE_DIR,'media')
# This is default path from Django, must be added
#AFTER our BASE_DIR otherwise DB will be broken.
BASE_DIR = Path(__file__).resolve().parent.parent
# add your apps to Installed apps
INSTALLED_APPS = [
'main',
'weblogin',
..........
]
# Now add TEMPLATE_DIR to 'DIRS' where in TEMPLATES like bellow
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR, BASE_DIR,],
'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',
],
},
},
]
# On end of Settings.py put this refferences to Static and Media files
STATICFILES_DIRS = [STATIC_DIR,]
STATIC_URL = '/static/'
MEDIA_ROOT = [MEDIA_DIR,]
MEDIA_URL = '/media/'
如果您遇到数据库问题,请检查是否将原始 BASE_DIR 置于新的 BASE_DIR 之下,否则请更改
# Original
'NAME': BASE_DIR / 'db.sqlite3',
# to
'NAME': os.path.join(BASE_DIR,'db.sqlite3'),
Django 现在能够在 App 文件夹和 Root 文件夹中找到 HTML 和静态文件,而无需在文件前面添加 App 文件夹的名称。
Struture:
-DjangoProject
-static(css,JS ...)
-templates(base.html, ....)
-other django files like (manage.py, ....)
-App1
-templates(index1.html, other html files can extend now base.html too)
-other App1 files
-App2
-templates(index2.html, other html files can extend now base.html too)
-other App2 files
解决方案 28:
简单的解决方案
'DIRS': [BASE_DIR, '模板'],
解决方案 29:
我的问题是我更改了应用程序的名称。毫不奇怪,Visual Studio 没有更改包含模板的目录名称。手动更正解决了该问题。
解决方案 30:
“模板不存在”错误的另一个原因似乎是忘记在 settings.py 中添加应用程序名称。我忘记添加它了,这就是我的情况导致错误的原因。
INSTALLED_APPS = [
'my_app',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]