【问题标题】:TemplateDoesNotExist at /polls/ index.html/polls/index.html 处的 TemplateDoesNotExist
【发布时间】:2014-03-11 14:16:34
【问题描述】:

我正在做一个关于 django 项目的教程。 实际上是早上 4.51,我只想让它工作。

我的 urls.py 文件:

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'^polls/$', 'polls.views.index'),
)

我的views.py文件:

from django.shortcuts import render

# Create your views here.
from django.shortcuts import render_to_response


def index(request):
    return render_to_response('index.html')

在文件夹模板中我有 index.html 文件。 它向我显示了相同的TemplateDoesNotExist 错误,所以我做了一些研究并 I found this question 所以我在我的 settings.py 中添加了这段代码:

import os.path
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
TEMPLATE_DIRS = (
    os.path.join(SITE_ROOT, 'templates/'),
)

那么如何让它工作呢?

这是回溯:

    Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/polls/

Django Version: 1.6.1
Python Version: 3.3.3
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.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:
C:\Users\JD\PycharmProjects\MyDjangoApp\MyDjangoApp\templates\index.html

(文件不存在) 使用加载器 django.template.loaders.app_directories.Loader: C:\Python33\lib\site-packages\django\contrib\admin\templates\index.html (文件不存在) C:\Python33\lib\site-packages\django\contrib\auth\templates\index.html (文件不存在)

Traceback:
File "C:\Python33\lib\site-packages\django\core\handlers\base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\JD\PycharmProjects\MyDjangoApp\polls\views.py" in index
  8.     return render_to_response('index.html')
File "C:\Python33\lib\site-packages\django\shortcuts\__init__.py" in render_to_response
  29.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "C:\Python33\lib\site-packages\django\template\loader.py" in render_to_string
  162.         t = get_template(template_name)
File "C:\Python33\lib\site-packages\django\template\loader.py" in get_template
  138.     template, origin = find_template(template_name)
File "C:\Python33\lib\site-packages\django\template\loader.py" in find_template
  131.     raise TemplateDoesNotExist(name)

Exception Type: TemplateDoesNotExist at /polls/
Exception Value: index.html

【问题讨论】:

  • 是否有文件位于:C:\Users\JD\PycharmProjects\MyDjangoApp\MyDjangoApp\templates\index.html?
  • 不,路径中有两次 MyDjangoApp 但我只有一次

标签: python django


【解决方案1】:

改变

import os.path
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))

到

import os
SITE_ROOT = os.path.dirname(os.path.dirname(__file__))

如果您查看回溯,Python 正在您的 app 文件夹中寻找模板文件夹,我假设它与 settings.py 是同一个文件夹。

另一种方法是将模板文件夹复制到 MyDjangoAPP 文件夹中。

【讨论】:

  • 你的意思是,我可以将模板文件夹复制到 MyDjangoApp 文件夹,而不是像上面那样在 setting.py 中写任何东西吗?
  • 是的!这也意味着每次创建新应用程序时,您都必须为此特定应用程序创建一个新模板文件夹,并将该应用程序模板放在新创建的模板文件夹中。例如:\MyDjangoApp\app1\templates 和 \MyDjangoApp\app2\templates
猜你喜欢
  • 2014-07-04
  • 1970-01-01
  • 1970-01-01
  • 2015-03-01
  • 2014-07-05
  • 2017-09-25
  • 1970-01-01
  • 2015-07-13
  • 2016-08-27
相关资源
最近更新 更多