我正在做一個關於django項目的教程。 其實它早上是4.51,我想讓它工作。TemplateDoesNotExist at/polls/index.html
我的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 \站點包\的Django \的contrib \ ADMIN \模板\ index.html在 (文件不存在) C:\ Python33 \ LIB \站點包\ Django的\的contrib \權威性\模板\ 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'? – dm03514
不是有兩次MyDjangoApp的路徑,但我只有一次 – user2950593