我对 Django 还是个新手,我正在尝试创建一个小型网站作为练习。但是我目前遇到了这个错误。如果有人可以解释我哪里出错了并教我如何解决这个问题,那就太好了!我是新手,文档有时很难阅读 =[
如果还有什么需要补充的,请告诉我!
Environment:
Request Method: GET
Request URL: http://localhost:8000/home/
Django Version: 1.5.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles')
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/bradford/Development/Django/pub_pic/~/Development/Django/pub_pic/templates/homepage_template/home.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/home/bradford/Development/Django/django_1.5.1/local/lib/python2.7/site-packages/django/contrib/auth/templates/homepage_template/home.html (File does not exist)
Traceback:
File "/home/bradford/Development/Django/django_1.5.1/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/home/bradford/Development/Django/pub_pic/homepage/views.py" in index
9. return render(request,'homepage_template/home.html')
File "/home/bradford/Development/Django/django_1.5.1/local/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render
53. return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/home/bradford/Development/Django/django_1.5.1/local/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
170. t = get_template(template_name)
File "/home/bradford/Development/Django/django_1.5.1/local/lib/python2.7/site-packages/django/template/loader.py" in get_template
146. template, origin = find_template(template_name)
File "/home/bradford/Development/Django/django_1.5.1/local/lib/python2.7/site-packages/django/template/loader.py" in find_template
139. raise TemplateDoesNotExist(name)
Exception Type: TemplateDoesNotExist at /home/
Exception Value: homepage_template/home.html
我有一个名为 home.html 的模板,它位于目录 pub_pic/templates/homepage_template/home.html
我的 pub_pic urls.py:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'home/',include('homepage.urls', namespace = 'home')),
)
我的主页 urls.py:
from django.conf.urls import patterns, url
from homepage import views
urlpatterns = patterns('',
url(r'^$', views.index, name = 'index'),
)
主页/views.py:
from django.http import HttpResponse
from django.template import RequestContext, loader
from django.shortcuts import render, render_to_response
#from homepage.models import
def index(request):
# template = loader.get_template('homepage/index.html')
return render(request,'homepage_template/home.html')
请您参考如下方法:
请不要按照接受的答案建议在设置中包含完整路径。这是“反 django”的做事方式,即使它确实有效。
在 Django 中,您可以为一个项目使用一个模板文件夹,也可以为每个应用程序使用一个模板文件夹。后者允许您将应用程序从一个项目移动到另一个项目(这是它们应该的工作方式),但前者可以为单一的一次性项目提供简单性。
您不需要也不应该指定绝对路径。在您的设置中,如果您想要为您的项目使用单个模板目录,请使用类似以下内容的内容:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates')
,)
这基本上是说您的模板位于主 BASE_DIR 路径中名为“templates”的文件夹中。在此示例中,这将是 manage.py 所在的根目录。现在只需在这个根目录中创建一个名为“templates”的文件夹,然后将您的 HTML 放入其中(如果您愿意,可以安排在子文件夹中)。
然后可以像
一样简单地加载模板templt = get_template('mytemplate.html)
假定 mytemplate.html 文件直接位于根目录下的 templates/文件夹中。您可以使用子文件夹,如果您这样做,您应该在引号中指定它们,例如'mysubdir/mytemplate.html'
作为替代方案,您可以允许每个应用程序拥有自己的模板 在这种情况下,您必须在设置中具有以下内容:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
这里的重点是
'APP_DIRS': True,
这 ( according to the Django docs ) 指示系统在每个应用程序中查找"template"文件夹。您的项目将类似于(假设项目名称为 mybigproject)
/home/myaccount/mybigproject/myapp1/templates/
和
/home/myaccount/mybigproject/myapp2/templates/
相比之下,在第一种情况下(单个模板文件夹)它只是
/home/myaccount/mybigproject/templates/
然而,需要注意的重要一点是您STILL REFERENCE IT AS:
templt = get_template('mytemplate.html)
Django 将为您搜索所有应用程序文件夹。 如果它仍然给你一个找不到文件的错误,那是配置问题。
如果您指定了完整路径,现在您需要在移动 PC(例如共享项目)、项目或应用程序时更改该路径,这是一场灾难。
