我最近试图学习Django作为我的一个私人项目。 当来到在模板中的第一章,Django的书推荐使用下面的代码片断Django Developments Template_DIRS设置失败
TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__),'templates').replace('\\','/'),)
然而,当我打开文件setting.py我发现没有像“template_dir模板”设置在settings.py模板路径,但列表:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),],
'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',
],
},
},
]
相关的关键 “DIR” 的值,是一个空列表。所以我尝试填充上面显示的内容。
然后,代码的东西在views.py(所有进口已完成)与setting.py同一文件夹
def current_datetime(request):
now = datetime.datetime.now()
t = get_template("current_datetime.html")
html = t.render(Context({"current_date" : now}))
return HttpResponse(html)
然后MKDIR模板,在模板文件夹中保存current_datetime.html
最后,运行project.and得到了在我的终端的消息:
警告: :(1_8.W001)的独立TEMPLATE_ *设置在Django 1.8中弃用>和TEMPLATES字典优先。您必须将以下设置的值放入您的默认模板字典:TEMPLATE_DIRS。
系统检查发现1个问题(0沉默)。 2017年6月15日 - 15:32:49 Django版本1.11.2,使用设置'mysite.settings' 在127.0.0.1:8000/处启动开发服务器使用CONTROL-C退出服务器。
当我打开Safari浏览器的地址(127.0.0.1:8000/time/),这里传来 错误消息:
任何帮助,请?
浏览了很多,但发现没有什么帮助.. – Elias
我的django版本:(1.11.2,u“final”,0)我的python版本:2。7 – Elias
我一开始没有看到你的错误图像,你应该编辑你的文章,以正确地嵌入它或从中复制文本 – SebCorbin