【发布时间】: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 但我只有一次