如何修复 Django 应用程序的 urls.py 以便不再收到 TemplateDoesNotExist 异常?
我目前正在关注一个有点可疑的 Django 教程,名为 60 分钟内使用 Django 完成一个完整的博客引擎,但我停留在第 6 页。所以我有一个名为 blog 的 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 尝试在 /home/sez/blog/templates/blogengine/' 目录中查找
/home/sez/blog/templates/blog/' 目录,但我仍然不完全理解 post_list.html
模板。为了让它工作,我必须让 Django 查看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')),
告诉将控制权重定向到我的 blogengine
目录中的 url.py
文件。这是该文件的代码
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'
),
)
那么我必须对我的 urls.py
文件进行哪些编辑才能让 Django 找到并呈现 post_list.html 模板?
I am currently following along with a somewhat questionable Django tutorial called A complete blog engine using Django in 60 minutes and am stuck on page 6. So I have a Django project called blog and an inside I've an app called blogengine. Currently I am getting a TemplateDoesNotExist
exception when I try loading
127.0.0.1:8000/blog/
Here is the full Traceback:
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
In the Template Loader Error
you can see Django tries to look for the post_list.html
template in the /home/sez/blog/templates/blogengine/' directory. To get this working I have to make Django look in the
/home/sez/blog/templates/blog/' directory but I still do not fully understand how URLconf
works.
Below are my 2 url.py
files. The first is my project level 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/(.*)
As I understand it, the line (r'^blog/', include('blog.blogengine.urls')),
is telling redirecting control over to the url.py
file in my blogengine
directory. Here is the code for that file
from django.conf.urls.defaults import *
from django.views.generic import list_detail
from blog.blogengine.views import Post
urlpatterns = patterns('',
url(r'^
So how what edits would I have to make to my urls.py
files in order to get Django to find and render the post_list.html
template?
,
'django.views.static.serve',
{'document_root': '/home/siddhion/blog/static/'}),
(r'^(?P<template>\w+)
As I understand it, the line (r'^blog/', include('blog.blogengine.urls')),
is telling redirecting control over to the url.py
file in my blogengine
directory. Here is the code for that file
So how what edits would I have to make to my urls.py
files in order to get Django to find and render the post_list.html
template?
, direct_to_template),
(r'^
As I understand it, the line (r'^blog/', include('blog.blogengine.urls')),
is telling redirecting control over to the url.py
file in my blogengine
directory. Here is the code for that file
So how what edits would I have to make to my urls.py
files in order to get Django to find and render the post_list.html
template?
, static_page, {'template':'base'}),
(r'^blog/', include('blog.blogengine.urls')),
url(r'^(?P<template>\w+)/
As I understand it, the line (r'^blog/', include('blog.blogengine.urls')),
is telling redirecting control over to the url.py
file in my blogengine
directory. Here is the code for that file
So how what edits would I have to make to my urls.py
files in order to get Django to find and render the post_list.html
template?
, static_page, name='static_page'),
)
As I understand it, the line (r'^blog/', include('blog.blogengine.urls')),
is telling redirecting control over to the url.py
file in my blogengine
directory. Here is the code for that file
So how what edits would I have to make to my urls.py
files in order to get Django to find and render the post_list.html
template?
,
list_detail.object_list,
{
'queryset': Post.objects.all(),
'template_object_name':'post',
},
name='blog_home'
),
)
So how what edits would I have to make to my urls.py
files in order to get Django to find and render the post_list.html
template?
As I understand it, the line (r'^blog/', include('blog.blogengine.urls')),
is telling redirecting control over to the url.py
file in my blogengine
directory. Here is the code for that file
So how what edits would I have to make to my urls.py
files in order to get Django to find and render the post_list.html
template?
As I understand it, the line (r'^blog/', include('blog.blogengine.urls')),
is telling redirecting control over to the url.py
file in my blogengine
directory. Here is the code for that file
So how what edits would I have to make to my urls.py
files in order to get Django to find and render the post_list.html
template?
As I understand it, the line (r'^blog/', include('blog.blogengine.urls')),
is telling redirecting control over to the url.py
file in my blogengine
directory. Here is the code for that file
So how what edits would I have to make to my urls.py
files in order to get Django to find and render the post_list.html
template?
As I understand it, the line (r'^blog/', include('blog.blogengine.urls')),
is telling redirecting control over to the url.py
file in my blogengine
directory. Here is the code for that file
So how what edits would I have to make to my urls.py
files in order to get Django to find and render the post_list.html
template?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设文件
/home/sez/blog/templates/blog/post_list.html
存在,您只需告诉 Django 在哪里查找它。显然,它会在/home/sez/blog/templates/blogengine/
中查找您的模板。您可以通过调整settings.py
中的TEMPLATE_DIRS
来更改它:但是,通常的做法是将模板子目录命名为其相应的应用程序,因此您可能需要考虑移动将您的模板发送到
blogengine
,Django 认为它们目前位于其中。Assuming that the file
/home/sez/blog/templates/blog/post_list.html
exists, you only have to tell Django where to look for it. Apparently, it looks for your templates in/home/sez/blog/templates/blogengine/
. You can change that by adjustingTEMPLATE_DIRS
in yoursettings.py
:However, it is common practice to name the template subdirectories as their corresponding apps, so you might want to consider moving your templates to
blogengine
, where Django assumes they are at the moment.您可以做两件事,要么通过传入
template_name
参数明确告诉它哪个模板,要么将post_list.html
移动到/blog/blog/templates/< /code> 目录,django 将在其中查找它(即使您没有在
TEMPLATE_DIRS
中指定位置) href="https://docs.djangoproject.com/en/dev/ref/generic-views/?from=olddocs#django-views-generic-list-detail-object-list" rel="nofollow">有关的文档通用视图:
You can do two things, either explicitly tell it which template by passing in the
template_name
parameter, or movepost_list.html
into/blog/blog/templates/
directory, which is where django will look for it (even if you don't specify the location inTEMPLATE_DIRS
)From the documentation on generic views: