Python Django 找不到我的模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4065045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django Can't Find My Templates
提问by duffymo
I'm running Python 2.6.1 and Django 1.2.1 on Windows XP SP3. I'm using JetBrains PyCharm 1.0 to create and deploy my Django apps.
我在 Windows XP SP3 上运行 Python 2.6.1 和 Django 1.2.1。我正在使用 JetBrains PyCharm 1.0 创建和部署我的 Django 应用程序。
I'm relatively inexperienced with Python, and I'm starting to learn Django by following along with "Writing Your First Django App" from the web site - the poll application. I'm stuck on part 3.
我对 Python 的经验相对较少,我开始通过跟随网站上的“编写您的第一个 Django 应用程序” - 轮询应用程序来学习 Django。我被困在第 3 部分。
Everything is fine when I add the simple callback functions for "Writing your first view".
当我为“编写您的第一个视图”添加简单的回调函数时,一切都很好。
I hit the snag when I get to "Write views that actually do something."
当我开始“编写实际执行某些操作的视图”时,我遇到了障碍。
I followed the instructions to modify the index view:
我按照说明修改索引视图:
- Add a new method to views.py (Note - template is ready from 'polls/index.html'):
- Add index.html template to
site-templates/polls/folder - Modify settings.py to point to
site-templatesfolder
- 向 views.py 添加一个新方法(注意 - 模板已从“polls/index.html”准备就绪):
- 将 index.html 模板添加到
site-templates/polls/文件夹 - 修改 settings.py 指向
site-templates文件夹
Here's the code in my views.py:
这是我的 views.py 中的代码:
from django.template import Context, loader
from polls.models import Poll
from django.http import HttpResponse
def index(request):
latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
t = loader.get_template('polls/index.html')
c = Context({
'latest_poll_list': latest_poll_list,
})
return HttpResponse(t.render(c))
Here's the line in my settings.py:
这是我的 settings.py 中的行:
TEMPLATE_DIRS = ('/site-templates/')
But still I get this message when I run:
但是当我运行时我仍然收到这条消息:
TemplateDoesNotExist at /polls/
polls/index.html
Request Method: GET
Request URL: http://localhost:8000/polls/
Django Version: 1.2.1
Exception Type: TemplateDoesNotExist
Exception Value:
polls/index.html
The exception is thrown in loader.py. My debug settings look like this:
在 loader.py 中抛出异常。我的调试设置如下所示:
TEMPLATE_CONTEXT_PROCESSORS
('django.core.context_processors.auth', 'django.core.context_processors.request')
TEMPLATE_DEBUG
True
TEMPLATE_DIRS
('/site-templates',)
TEMPLATE_LOADERS
('django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader')
My directory structure looks like this:
我的目录结构如下所示:


What did I miss? Is the settings.py incorrect? Please advise.
我错过了什么?settings.py 不正确吗?请指教。
采纳答案by zsquare
You must use absolute paths in the TEMPLATE_DIRSsetting.
您必须在TEMPLATE_DIRS设置中使用绝对路径。
Convenient thing to do, at the top of your settings, insert:
方便的事情,在您的设置顶部,插入:
import os
DIRNAME = os.path.abspath(os.path.dirname(__file__))
Then anywhere you use a path, use os.path.join.
Example, your TEMPLATE_DIRSwould become:
然后在任何使用路径的地方,使用os.path.join. 例如,你TEMPLATE_DIRS会变成:
TEMPLATE_DIRS = (
os.path.join(DIRNAME, 'site-templates/'),
)
回答by seriyPS
http://docs.djangoproject.com/en/1.2/ref/templates/api/#loading-templatesSmall fix for @zsquare answer:
http://docs.djangoproject.com/en/1.2/ref/templates/api/#loading-templates@zsquare 答案的小修复:
import os
DIRNAME = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIRS = ( DIRNAME+'/site-templates/' )
回答by raviteja
This will surely solve the issue, instead of all the above try adding only below line in settings.py:
这肯定会解决问题,而不是以上所有尝试仅在以下行中添加settings.py:
TEMPLATE_DIRS = (
"appname/templates",
)
回答by michael-mammut
I was faced to the same problem. The mistake in my case was, that the 'app' was not in the INSTALLED_APPSlist at the project settings.py file.
我面临同样的问题。我的错误是,“应用程序”不在INSTALLED_APPS项目 settings.py 文件的列表中。
The error raise an error message they suggests similar error.
该错误引发了一条错误消息,他们暗示了类似的错误。
line 25, in get_template TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: authControll/index.html
settings.py --> Application definition
settings.py --> 应用定义
INSTALLED_APPS = [
...,
'authControll'
]
回答by Leonardo Leano
Django has a sort of patterns and philosophy. Try to use the same configurations other wise you have to change the core patterns in django.
Django 有一种模式和哲学。尝试使用相同的配置,否则您必须更改 django 中的核心模式。
The pattern for templates in django are like this:
django 中模板的模式是这样的:
polls/templates/polls/index.html
But to use it you have to add the installed app at the configs:
但是要使用它,您必须在配置中添加已安装的应用程序:
INSTALLED_APPS = [
'polls.apps.PollsConfig', #<-- Here this shoud be solve it
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',]
For more information look at:
有关更多信息,请查看:
https://docs.djangoproject.com/en/3.0/intro/tutorial02/#activating-models
https://docs.djangoproject.com/en/3.0/intro/tutorial02/#activation-models

