如何修复 Django 应用程序的 urls.py 以便不再收到 TemplateDoesNotExist 异常?

发布于 2024-12-21 08:15:50 字数 3751 浏览 1 评论 0原文

我目前正在关注一个有点可疑的 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/' 目录中查找 post_list.html 模板。为了让它工作,我必须让 Django 查看/home/sez/blog/templates/blog/' 目录,但我仍然不完全理解 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?

, '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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七月上 2024-12-28 08:15:50

假设文件 /home/sez/blog/templates/blog/post_list.html 存在,您只需告诉 Django 在哪里查找它。显然,它会在 /home/sez/blog/templates/blogengine/ 中查找您的模板。您可以通过调整 settings.py 中的 TEMPLATE_DIRS 来更改它:

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 认为它们目前位于其中。

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 adjusting TEMPLATE_DIRS in your 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',
)

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.

轮廓§ 2024-12-28 08:15:50

您可以做两件事,要么通过传入 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">有关的文档通用视图:

模板名称:

如果未指定 template_name,该视图将使用模板
默认情况下,/_list.html。

You can do two things, either explicitly tell it which template by passing in the template_name parameter, or move post_list.html into /blog/blog/templates/ directory, which is where django will look for it (even if you don't specify the location in TEMPLATE_DIRS)

From the documentation on generic views:

Template name:

If template_name isn't specified, this view will use the template
<app_label>/<model_name>_list.html by default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文