Django模板问题

时间:2010-11-02 15:41:15

标签: django

我正在关注this教程,在我添加模板并将模式放在urls.py中后,我进入了网站,我得到的模板未加载页面。

这是来自settings.py:

的模板目录部分
TEMPLATE_DIRS = (
    "/home/django-projects/base/templates/",
)

基础文件夹层次结构是这样的:

blog<dir>
dbs<dir>
__init__.py
manage.py
settings.py
templates<dir>
urls.py

urls.py看起来像这样:

from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('base.blog.views',
    (r"", "main"),
    # Example:
    # (r'^base/', include('base.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    (r'^admin/', include(admin.site.urls)),
)

在模板中,我有一个名为blog的文件夹,它包含教程中的两个html文件。我只是不确定我缺少什么设置来显示正确的模板。我很乐意发布您需要查看的任何其他内容。我确信这是微不足道的,我似乎无法弄清楚它是什么。谢谢你的帮助。

修改 所以我正在玩它,我意识到我无法进入管理员。因此,我将博客部分的网址与主要部分分开。顶层的urls.py看起来像这样:

from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'blog/', include('blog.urls')),
    # Example:
    # (r'^base/', include('base.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    (r'^admin/', include(admin.site.urls)),
)

在博客文件夹中,urls.py看起来像这样:     来自django.conf.urls.defaults import *

urlpatterns = patterns('base.blog.views',
    (r'^$', 'main'),
)

现在我可以访问管理员,但在我尝试访问博客时仍然会收到模板加载错误。

TemplateDoesNotExist at /blog/
blog/list.html
Request Method: GET
Request URL:    http://192.168.1.124:9999/blog/
Django Version: 1.2.3
Exception Type: TemplateDoesNotExist
Exception Value:    
blog/list.html
Exception Location: /usr/local/lib/python2.6/dist-packages/Django-1.2.3-py2.6.egg/django/template/loader.py in find_template, line 138
Python Executable:  /usr/bin/python
Python Version: 2.6.6
Python Path:    ['/home/kevin/django-projects/base', '/usr/local/lib/python2.6/dist-packages/setuptools-0.6c11-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/BeautifulSoup-3.1.0.1-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/Django-1.2.3-py2.6.egg', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0']
Server time:    Tue, 2 Nov 2010 11:59:13 -0500

Template-loader postmortem

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:

2 个答案:

答案 0 :(得分:2)

“主要”视图的最后一行是否应包含“blog / list.html”而不是“list.html”?

尝试使用以下内容替换该行:

return render_to_response("blog/list.html", dict(posts=posts, user=request.user))

使用“发布”视图执行相同的操作。

基本上,Django将查看settings.py中每个已配置的模板目录,如果指定“list.html”,则最好是在这些目录的根目录中。由于您将“list.html”放在“blog”子目录中而不是您告诉它的位置,Django无法找到它。

答案 1 :(得分:0)

首先,您的模板目录中缺少前导/:

TEMPLATE_DIRS = (
    "/home/django-projects/base/templates/",
)