Django 找不到我的模板
我在 Windows XP SP3 上运行 Python 2.6.1 和 Django 1.2.1。我正在使用 JetBrains PyCharm 1.0 创建和部署我的 Django 应用程序。
我对 Python 相对缺乏经验,并且我开始按照网站上的“编写您的第一个 Django 应用程序”(投票应用程序)来学习 Django。我陷入了第 3 部分。
当我为“编写你的第一个视图”添加简单的回调函数时,一切都很好。
当我谈到“编写实际做某事的视图”时,我遇到了障碍。
我按照说明修改索引视图:
- 向views.py添加新方法(注意-模板已从“polls/index.html”准备就绪):
- 将index.html模板添加到
site-templates/polls/< /code> 文件夹
- 修改 settings.py 以指向
site-templates
文件夹
这是我的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))
这是我的settings.py中的行:
TEMPLATE_DIRS = ('/site-templates/')
但是当我运行时我仍然收到此消息:
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
异常在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')
我的目录结构如下所示:
我错过了什么?是不是settings.py不正确?请指教。
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.
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.
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-templates
folder
Here's the code in my 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:
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:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您必须在
TEMPLATE_DIRS
设置中使用绝对路径。方便的做法是,在设置顶部插入:
然后在任何使用路径的地方,使用 os.path.join。
例如,您的
TEMPLATE_DIRS
将变为:You must use absolute paths in the
TEMPLATE_DIRS
setting.Convenient thing to do, at the top of your settings, insert:
Then anywhere you use a path, use
os.path.join
.Example, your
TEMPLATE_DIRS
would become:Django 有一种模式和哲学。尝试使用相同的配置,否则您必须更改 django 中的核心模式。
django 中模板的模式如下:
但是要使用它,您必须在配置中添加已安装的应用程序:
有关更多信息,请参阅:
https://docs.djangoproject.com/en/3.0/intro/tutorial02/#activating-models
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.
The pattern for templates in django are like this:
But to use it you have to add the installed app at the configs:
For more information look at:
https://docs.djangoproject.com/en/3.0/intro/tutorial02/#activating-models
我的错误只是定位不当:
我的解决方案是:
My mistake was simply bad positioning:
My solution was :
这肯定会解决问题,而不是上述所有尝试在
settings.py
中仅添加以下行:This will surely solve the issue, instead of all the above try adding only below line in
settings.py
:http://docs.djangoproject.com/en/1.2/参考/模板/api/#loading-templates
对 @zsquare 答案的小修复:
http://docs.djangoproject.com/en/1.2/ref/templates/api/#loading-templates
Small fix for @zsquare answer:
我面临着同样的问题。我的错误是,“应用程序”不在项目 settings.py 文件的 INSTALLED_APPS 列表中。
该错误会引发一条错误消息,它们表明类似的错误。
settings.py -->应用定义
I was faced to the same problem. The mistake in my case was, that the 'app' was not in the
INSTALLED_APPS
list at the project settings.py file.The error raise an error message they suggests similar error.
settings.py --> Application definition