2014-02-12 146 views
0

我正在做一个关于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 
+0

是否有文件位于:'C:\ Users \ JD \ PycharmProjects \ MyDjangoApp \ MyDjangoApp \ templates \ index.html'? – dm03514

+0

不是有两次MyDjangoApp的路径,但我只有一次 – user2950593

回答

2

变化

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

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

如果你看看你的回溯,Python是找你的应用程序文件夹中的模板文件夹,我认为这将是相同的文件夹设置的.py。

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

+0

谢谢)))))我真的很高兴))) – user2950593

+0

你的意思是,我可以复制模板文件夹到MyDjangoApp文件夹,不写在setting.py像上面的东西? Yup! – user2950593

+0

Yup!这也意味着,每次创建新应用程序时,都必须为该特定应用程序创建一个新的模板文件夹,并将该应用程序模板放入新创建的模板文件夹中。 例如: \ MyDjangoApp \ app1 \ templates 和 \ MyDjangoApp \ app2 \ templates – Hevlastka