【发布时间】:2016-06-07 22:00:42
【问题描述】:
我正在使用linux、python 3.4、django 1.8.2、pytmysql
在我的 virtualenv 中有:
db.sqlite3manage.pynew/templates/
settings.py:
首先我评论了'DIRS':[],
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
#'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
然后添加这部分:
TEMPLATE_DIRS = (
'/home/niloofar/django/niloofar/new/templates',
)
urls.py:
从 new.views 导入欢迎
urlpatterns = [
url(r'^welcome', welcome),
]
views.py:
from django.shortcuts import render
def welcome(request):
message = "welcome again:D"
return render(request, 'base.html', {'message': message})
在templates目录下,有base.html文件:
<html>
<body>
<p>* {{ message }} *</p>
</body>
</html>
当我刷新页面时,它会打印错误:
异常类型:TemplateDoesNotExist
异常值:base.html
【问题讨论】:
-
base.html的完整路径是什么? -
/home/niloofar/django/niloofar/new/templates/base.html
-
试试 $ ls -a /home/niloofar/django/niloofar/new/templates。你得到了什么?
-
为什么使用
TEMPLATE_DIRS?自 1.8 起已弃用。 docs.djangoproject.com/en/1.9/ref/settings/#template-dirs -
不要注释掉
dirs,而是尝试将其设置为包含template_dirs中的目录的数组。
标签: python django django-templates