Django找不到应用程序模板 [英] Django Can't Find App Templates

查看:276
本文介绍了Django找不到应用程序模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



工作区是我的主目录中的一个文件夹我的路径是〜workspace / mysite /



和项目结构是

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

I只需列出每个文件夹的内容,以简化:




  • db.sqlite3 - 是一个文件

  • manage.py - 是一个文件

  • mysite - 是项目文件夹

  • mysite_env - 是virtualenv文件夹

  • polls - 是应用程序文件夹,包含一个文件目录结构,其中的模板未被拾取。打开目录中的结构是templates / polls / index.html

  • templates - 是项目模板目录。在我指定的mysite / settings.py中,它正在被django



拾取

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

polls / templates / polls / index.html包含:

 `{%if latest_question_list%} 
< ul>
{%new_question_list%}中的问题
< li>< a href =/ polls / {{question.id}} /> {{question.question_text}}< /一个>一种与GT;< /锂>
{%endfor%}
< / ul>
{%else%}
< p>没有可用的投票。< / p>
{%endif%}`

最后但并非最不重要的是,polls / urls.py包含用于/ polls /的匹配索引的正则表达式:

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

得到的是:

 `TemplateDoesNotExist at / polls / 
/polls/index.html
请求方法:GET
请求URL:http://127.0.0.1:8000/polls/
Django版本:1.7.4
异常类型:TemplateDoesNotExist
异常值:
/polls/index.html
异常位置:/home/jeremiah/workspace/mysite/mysite_env/lib/python3.4/site-packages/django/template/loader.py在find_template中,第136行
Python可执行文件:/ home / jeremiah / workspace / mysite / mysite_env / bin / python
Python版本:3.4.0
Python路径:
['/ home / jeremiah / workspace / mysite'
'/home/jeremiah/workspace/mysite/mysite_env/lib/python3.4',
'/ home / jeremi啊/ 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']
服务器时间:Wed,4 Feb 2015 18:27:56 -0800`

它所提供的追溯是:

 `nvironment:


请求方法:GET
请求URL:http://127.0.0.1:8000/polls/

Django版本:1.7.4
Python版本:3.4.0
已安装的应用程序:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes'
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls')
已安装的中间件:
('django.contrib.sessions.middleware.SessionMiddleware',
'dj ango.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth。
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')

模板加载器错误:
Django尝试按以下顺序加载这些模板:
使用loader django.template.loaders.filesystem.Loader:
使用loader django.template.loaders.app_directories.Loader:



追溯:
文件/home/jeremiah/workspace/mysite/mysite_env/lib/python3.4/site-packages/django/core/handlers/base.py中的get_response
111. response = wrapped_callback(request,* callback_args,** callback_kwargs)
索引
中的文件/home/jeremiah/workspace/mysite/polls/views.py9. template = loader.get_template( '/调查/ index.html的')
文件/home/jeremiah/workspace/mysite/mysite_env/lib/python3.4/site-packages/django/template/loader.pyin get_template
144. template,origin = find_template(template_name ,dirs)
文件/home/jeremiah/workspace/mysite/mysite_env/lib/python3.4/site-packages/django/template/loader.py在find_template
136. raise TemplateDoesNotExist(name )

异常类型:TemplateDoesNotExist at / polls /
异常值:/ polls / index.html`

有人可以帮我吗?我一直在试图找出错误的方式太久了。欢迎任何基于目前公认的做法的想法。



谢谢。

解决方案

删除第一个 / 从模板名称斜杠。它应该是:

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


I'm working through the official django 1.7 tutorial found here. Everything is going smoothly except django can't find the templates for apps. It finds templates that I put under workspace/mysite/templates but not under workspace/mysite/polls/templates.

workspace is a folder I have in my home directory where I'm keeping all my web projects.

My path is ~workspace/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
  • manage.py - is a file
  • mysite - is the project folder
  • mysite_env - is the virtualenv folder
  • polls - is the app folder and contains a file directory structure with the templates that aren't being picked up. The structure within the open directory is templates/polls/index.html
  • templates - is the project template dir. It is being picked up by django

in my mysite/settings.py I have specified

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

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

`{% 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/:

`#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')

这篇关于Django找不到应用程序模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆