python – Django无法找到应用程序模板

我正在通过官方的django 1.7教程找到here.除了django找不到应用程序的模板外,一切都很顺利.它找到我放在workspace / mysite / templates下但不在workspace / mysite / polls / templates下的模板.

workspace是我在我的主目录中的一个文件夹,我保留了所有的web项目.

我的路径是〜工作区/ mysite /

而项目结构是

`workspace
  |
  mysite
     |
     db.sqlite3 - manage.py - mysite - mysite_env - polls - templates`

为简洁起见,我只列出每个文件夹的内容:

> db.sqlite3 – 是一个文件
> manage.py – 是一个文件
> mysite – 是项目文件夹
> mysite_env – 是virtualenv文件夹
> polls – 是app文件夹,包含一个文件目录结构,其中包含未被拾取的模板.打开目录中的结构是templates / polls / index.html
> templates – 是项目模板目录. django正在接收它

在我指定的mysite / settings.py中

`TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]`

我尝试添加一个路径到polls / templates文件夹,但这也没有用.

index的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))`

实际的polls / 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`

有人可以帮我这个吗?我一直试图弄清楚出现什么问题.我们欢迎任何基于目前公认的做法的想法.

谢谢.

最佳答案
从模板名称中删除第一个/斜杠.它应该是:

template = loader.get_template('polls/index.html')

转载注明原文:python – Django无法找到应用程序模板 - 代码日志