2014-02-23 168 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