2014-02-23 170 views
0

不是我第一次得到这个错误,但通常我通过向TEMPLATE_DIRS元组添加模板路径来解决它,尽管这不再适用于我。这里是我的settings.py模板片段:Django抛出TemplateDoesNotExist

TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". 
# Always use forward slashes, even on Windows. 
# Don't forget to use absolute paths, not relative paths. 
os.path.join(os.path.dirname(__file__), '..', 'templates').replace('\\','/'), 
) 

我安装的应用程序片段:

INSTALLED_APPS = (
'django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.sites', 
'django.contrib.messages', 
'django.contrib.staticfiles', 
# Uncomment the next line to enable the admin: 
'django.contrib.admin', 
# Uncomment the next line to enable admin documentation: 
'django.contrib.admindocs', 
#this is my app right here. 
'boilerplate', 
) 

而简单的代码,我在我的views.py文件:

from django.shortcuts import render_to_response 
from django.template import RequestContext 

def index(request): 
return render_to_response('test.html',context_instance=RequestContext(request)) 

我相信它很重要的一点是我的模板文件夹放在我的项目结构中,而不是我的应用程序中。有任何想法吗?

+1

也没关系,你的模板是在项目目录。什么是打印'os.path.join(os.path.dirname(__ file__),'..','templates')。replace('\','/')'? – agconti

+1

另外,你有没有厌倦把绝对文件路径作为一个字符串在模板元组中查看是否有效 – agconti

+0

只是尝试了第二个建议,它没有奏效。错误屏幕显示以下消息:“/home/user/PyWeb/django_backbone/templates/test.html”< - 这是正确的路径。我可以在我的控制台中看到test.html。 – user1659653

回答

1

这是您需要实现的文件夹结构。

myproject/ 
    ... 
    myapp/ 
    views.py 
    ... 
    templates/ 
     myapp/ 
     mytemplate.html 

也就是说你在views.py做什么

def index(request): 
return render_to_response('myapp/test.html',context_instance=RequestContext(request)) 

你的TEMPLATE_DIRS是好的。

阅读此了解更多信息:https://docs.djangoproject.com/en/1.6/intro/tutorial03/#write-views-that-actually-do-something

+1

我完全忘了这个线程。是的,我有错误的文件夹结构,我在这个星期前解决了,但这是正确的答案。感谢发布。 – user1659653