不是我第一次得到這個錯誤,但通常我通過向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))
我相信它很重要的一點是我的模板文件夾放在我的項目結構中,而不是我的應用程序中。有任何想法嗎?
也沒關係,你的模板是在項目目錄。什麼是打印'os.path.join(os.path.dirname(__ file__),'..','templates')。replace('\','/')'? – agconti
另外,你有沒有厭倦把絕對文件路徑作爲一個字符串在模板元組中查看是否有效 – agconti
只是嘗試了第二個建議,它沒有奏效。錯誤屏幕顯示以下消息:「/home/user/PyWeb/django_backbone/templates/test.html」< - 這是正確的路徑。我可以在我的控制檯中看到test.html。 – user1659653