【问题标题】:Django Developments Template_DIRS setting failedDjango Developments Template_DIRS 设置失败
【发布时间】:2017-11-18 05:31:42
【问题描述】:

我最近尝试为我的一个私人项目学习 Django。 来到模板章节时,Django Book推荐在settings.py中设置模板路径,使用如下sn-p

TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__),'templates').replace('\\','/'),)

但是,当我打开文件 setting.py 时,我发现没有像“TEMPLATE_DIR”这样的东西,而是一个列表:

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 中编写一些代码(所有导入都已完成)

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 模板与 setting.py 在同一文件夹中,将 current_datetime.html 保存在文件夹模板中

最后,运行项目。并在我的终端中收到消息:

警告: ?: (1_8.W001) 独立的 TEMPLATE_* 设置在 Django 1.8 > 中已弃用,并且 TEMPLATES 字典优先。您必须将 > 以下设置的值放入默认的 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/) 时,出现了 错误信息:

enter image description here

有人帮忙吗??

【问题讨论】:

  • 浏览了很多,但发现没有任何帮助..
  • 我的django版本:(1.11.2, u"final",0)我的python版本:2.7
  • 我一开始没有看到你的错误图片,你应该编辑你的帖子以正确嵌入它或从中复制文本
  • 如果你的书包含TEMPLATE_DIRS,那么它已经过时了,你应该寻找不同的资源来学习 Django。如果一个资源是为 Django 1.8 之前的任何东西编写的,那么当你尝试将它与 Django 1.11 一起使用时就会遇到问题。

标签: python django templates


【解决方案1】:

在 TEMPLATE_DIRS 上有一个警告,因此请确保您的 settings.py 中没有 TEMPLATE_DIRS 变量并重新启动开发服务器。

那么对于错误,您实际上是在使用 Context 对象而不是 dict,您应该使用有用的快捷方式 https://docs.djangoproject.com/en/1.11/topics/http/shortcuts/#example 呈现模板

from django.shortcuts import render

def current_datetime(request):
  now = datetime.datetime.now()
  return render(request, "current_datetime.html", {
    'current_date' : now,
  })

【讨论】:

  • @SebCorbin 他有 TEMPLATE 字典,不需要定义特定的 TEMPLATE_DIRS
【解决方案2】:

我认为您在 settings.py 中有 BASE_DIR 用于定义位置,因此如下所示使用

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates'),],
    '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',
        ],
    },
},

DIRS 应该如下所示

'DIRS': [os.path.join(BASE_DIR, 'templates'),],

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-03
    • 2015-06-30
    • 1970-01-01
    • 2013-11-05
    • 2013-12-19
    • 2013-05-12
    • 1970-01-01
    • 2016-04-29
    相关资源
    最近更新 更多