不是我第一次得到这个错误,但通常我通过向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