简体   繁体   English

Django无法找到应用程序模板

[英]Django Can't Find App Templates

I'm working through the official django 1.7 tutorial found here . 我正在通过这里找到的官方django 1.7教程。 Everything is going smoothly except django can't find the templates for apps. 除了django找不到应用程序的模板外,一切都很顺利。 It finds templates that I put under workspace/mysite/templates but not under workspace/mysite/polls/templates. 它找到我放在workspace / mysite / templates下但不在workspace / mysite / polls / templates下的模板。

workspace is a folder I have in my home directory where I'm keeping all my web projects. workspace是我在我的主目录中的一个文件夹,我保留了所有的web项目。

My path is ~workspace/mysite/ 我的路径是〜工作区/ mysite /

and the project structure is 而项目结构是

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

I'll just list the contents of each folder for brevity: 为简洁起见,我只列出每个文件夹的内容:

  • db.sqlite3 - is a file db.sqlite3 - 是一个文件
  • manage.py - is a file manage.py - 是一个文件
  • mysite - is the project folder mysite - 是项目文件夹
  • mysite_env - is the virtualenv folder mysite_env - 是virtualenv文件夹
  • polls - is the app folder and contains a file directory structure with the templates that aren't being picked up. polls - 是app文件夹,包含一个文件目录结构,其中包含未被拾取的模板。 The structure within the open directory is templates/polls/index.html 打开目录中的结构是templates / polls / index.html
  • templates - is the project template dir. templates - 是项目模板目录。 It is being picked up by django django正在接收它

in my mysite/settings.py I have specified 在我指定的mysite / settings.py中

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

I tried adding a path to the polls/templates folder, but that didn't work either. 我尝试添加一个路径到polls / templates文件夹,但这也没有用。

polls/views.py settings for index is: 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))`

the actual polls/templates/polls/index.html contains: 实际的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 %}`

and last but not least, polls/urls.py contains this regex for matching index for /polls/: 最后但并非最不重要的是,polls / urls.py包含此正则表达式,用于匹配/ polls /的索引:

`#ex: /polls/
    url(r'^$', views.index, name='index'),`

The specific error I'm getting is: 我得到的具体错误是:

`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`

The traceback it's giving is: 它给出的追溯是:

`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`

Can someone please help me with this? 有人可以帮我这个吗? I've been trying to figure out what's wrong for way too long now. 我一直试图弄清楚出现什么问题。 Any ideas based on currently accepted practices are welcome. 我们欢迎任何基于目前公认的做法的想法。

Thank you. 谢谢。

Remove the first / slash from the template name. 从模板名称中删除第一个/斜杠。 It should be: 它应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM