我目前正在关注一个名为A complete blog engine using Django in 60 minutes and am stuck on page 6的有点可疑的Django教程。所以我有一个名为博客的Django项目,而我内部有一个名为 blogengine 的应用程序。目前,当我尝试加载
时,我收到TemplateDoesNotExist
个异常
127.0.0.1:8000/blog/
这是完整的回溯:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/
Django Version: 1.3.1
Python Version: 2.7.2
Installed Applications:
['blogengine',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/home/sez/blog/templates/blogengine/post_list.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/usr/local/lib/python2.7/site-packages/django/contrib/admin/templates/blogengine/post_list.html (File does not exist)
Traceback:
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/site-packages/django/views/generic/list_detail.py" in object_list
107. t = template_loader.get_template(template_name)
File "/usr/local/lib/python2.7/site-packages/django/template/loader.py" in get_template
157. template, origin = find_template(template_name)
File "/usr/local/lib/python2.7/site-packages/django/template/loader.py" in find_template
138. raise TemplateDoesNotExist(name)
Exception Type: TemplateDoesNotExist at /blog/
Exception Value: blogengine/post_list.html
在Template Loader Error
中你可以看到Django试图在post_list.html
/ home / sez / blog / templates / blog /'目录中查找/home/sez/blog/templates/blogengine/' directory. To get this working I have to make Django look in the
模板,但我仍然没有完全理解URLconf
的工作原理。
以下是我的2 url.py
个文件。第一个是我的项目级别url.py
from django.conf.urls.defaults import patterns, include, url
from django.views.generic.simple import direct_to_template
from blog.views import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
(r'^static/(.*)$',
'django.views.static.serve',
{'document_root': '/home/siddhion/blog/static/'}),
(r'^(?P<template>\w+)$', direct_to_template),
(r'^$', static_page, {'template':'base'}),
(r'^blog/', include('blog.blogengine.urls')),
url(r'^(?P<template>\w+)/$', static_page, name='static_page'),
)
根据我的理解,行(r'^blog/', include('blog.blogengine.urls')),
告诉我将控件重定向到url.py
目录中的blogengine
文件。这是该文件的代码
from django.conf.urls.defaults import *
from django.views.generic import list_detail
from blog.blogengine.views import Post
urlpatterns = patterns('',
url(r'^$',
list_detail.object_list,
{
'queryset': Post.objects.all(),
'template_object_name':'post',
},
name='blog_home'
),
)
那么为了让Django找到并呈现urls.py
模板,我必须对我的post_list.html
文件进行哪些编辑?
答案 0 :(得分:2)
假设文件/home/sez/blog/templates/blog/post_list.html
存在,您只需要告诉Django在哪里查找它。显然,它会在/home/sez/blog/templates/blogengine/
中查找您的模板。您可以通过调整TEMPLATE_DIRS
中的settings.py
来改变这种情况:
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
'/home/sez/blog/templates/blog',
)
但是,通常的做法是将模板子目录命名为相应的应用程序,因此您可能需要考虑将模板移动到blogengine
,Django假定它们现在在这里。
答案 1 :(得分:0)
你可以做两件事,要么通过传入template_name
参数明确告诉它哪个模板,要么将post_list.html
移动到/blog/blog/templates/
目录,这是django将寻找的目录(即使您未在TEMPLATE_DIRS
)
来自documentation on generic views:
模板名称:
如果未指定template_name,则此视图将使用该模板 &lt; app_label&gt; /&lt; model_name&gt; _list.html默认情况下。