我正在通过官方django 1.7教程找到here。除了django找不到应用程序的模板外,一切都很顺利。它找到我放在workspace / mysite / templates下但不在workspace / mysite / polls / templates下的模板。
工作区是我在我的主目录中的一个文件夹,我将保留所有的Web项目。
我的路径是〜工作区/ mysite /
,项目结构是
`workspace
|
mysite
|
db.sqlite3 - manage.py - mysite - mysite_env - polls - templates`
为简洁起见,我只列出每个文件夹的内容:
在我的mysite / settings.py中我指定了
`TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]`
我尝试在polls / templates文件夹中添加一个路径,但这也不起作用。
索引的polls / views.py设置为:
`def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
template = loader.get_template('/polls/index.html')
context = RequestContext(request, {
'latest_question_list': latest_question_list,
})
return HttpResponse(template.render(context))`
实际的民意调查/ templates / polls / index.html包含:
`{% if latest_question_list % }
<ul>
{% for question in latest_question_list %}
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a>a></li>
{% endfor %}
</ul>
{% else %}
<p>No Polls are available.</p>
{% endif %}`
最后但并非最不重要的是,polls / urls.py包含此正则表达式以匹配/ polls /的索引:
`#ex: /polls/
url(r'^$', views.index, name='index'),`
我得到的具体错误是:
`TemplateDoesNotExist at /polls/
/polls/index.html
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Django Version: 1.7.4
Exception Type: TemplateDoesNotExist
Exception Value:
/polls/index.html
Exception Location: /home/jeremiah/workspace/mysite/mysite_env/lib/python3.4/site-packages/django/template/loader.py in find_template, line 136
Python Executable: /home/jeremiah/workspace/mysite/mysite_env/bin/python
Python Version: 3.4.0
Python Path:
['/home/jeremiah/workspace/mysite',
'/home/jeremiah/workspace/mysite/mysite_env/lib/python3.4',
'/home/jeremiah/workspace/mysite/mysite_env/lib/python3.4/plat-x86_64-linux-gnu',
'/home/jeremiah/workspace/mysite/mysite_env/lib/python3.4/lib-dynload',
'/usr/lib/python3.4',
'/usr/lib/python3.4/plat-x86_64-linux-gnu',
'/home/jeremiah/workspace/mysite/mysite_env/lib/python3.4/site-packages']
Server time: Wed, 4 Feb 2015 18:27:56 -0800`
它给出的追溯是:
`nvironment:
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Django Version: 1.7.4
Python Version: 3.4.0
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
Traceback:
File "/home/jeremiah/workspace/mysite/mysite_env/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/jeremiah/workspace/mysite/polls/views.py" in index
9. template = loader.get_template('/polls/index.html')
File "/home/jeremiah/workspace/mysite/mysite_env/lib/python3.4/site-packages/django/template/loader.py" in get_template
144. template, origin = find_template(template_name, dirs)
File "/home/jeremiah/workspace/mysite/mysite_env/lib/python3.4/site-packages/django/template/loader.py" in find_template
136. raise TemplateDoesNotExist(name)
Exception Type: TemplateDoesNotExist at /polls/
Exception Value: /polls/index.html`
有人可以帮我这个吗?我一直试图弄清楚出现什么问题。我们欢迎任何基于目前公认的做法的想法。
谢谢。
答案 0 :(得分:5)
从模板名称中删除第一个/
斜杠。它应该是:
template = loader.get_template('polls/index.html')