【问题标题】:Django path to templates folder in settings.pysettings.py 中模板文件夹的 Django 路径
【发布时间】:2018-02-04 00:06:31
【问题描述】:

我刚开始学习 Django,正在学习 Learn Django 1.11 教程。

这是我当前的项目树:

├── manage.py
├── muypicky
│   ├── __init__.py
│   ├── old_settings.py
│   ├── settings
│   │   ├── base.py      # Contains the settings (like shown in the tutorial)
│   │   ├── __init__.py
│   ├── urls.py
│   └── wsgi.py
├── requirements.txt
├── restaurants
└── templates           # Templates Folder
└── index.html

我正在尝试将路径添加到设置文件夹中的模板文件夹。但是显示的错误

django.template.loaders.filesystem.Loader: .../muypicky/tempelates/index.html(源不存在)

当前的setting.py文件

TEMPLATES = [{
  'DIRS': [os.path.join(BASE_DIR, 'tempelates')], 
  },]

查看错误,文件路径错误,因为它进入了不正确的 /muypicky/tempelates。那么如何使用setting.py(base.py)中的给定文件树进入根文件夹,然后进入模板文件夹。

如有任何进一步的疑问,请尽管询问,非常感谢。

【问题讨论】:

标签: python django


【解决方案1】:

我有相同的项目树,我在 TEMPLATES 中放了:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': ['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',
            'django.template.context_processors.media',
        ],
    },
},

]

【讨论】:

    【解决方案2】:

    您可能拼错了名称,因为该目录名为 templates,但您的 settings.py 正在寻找 templates

    ├── manage.py
    ...
    └── templates           # Templates Folder
    └── index.html
    

    但是在你的设置中你说templates

    TEMPLATES = [{
      'DIRS': [os.path.join(BASE_DIR, 'templates')], 
      },]
    

    这就是 Djagno 找不到文件的原因

    .../muypicky/templates/index.html(来源不存在)

    【讨论】:

    • 还是不行。即使在修复拼写错误之后,我仍然得到'''muypicky/templates/index.html(源不存在)'''。我认为这与 base.py 中的 BASE_DIR 更相关,就像这样:'''BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file) ))'''
    • 还要注意,当我将系统上的确切路径添加到模板文件夹时,它确实有效,这就是为什么我认为是我的 BASE_DIR 导致了问题。
    • 哈哈,您将他拼错的模板包含在内的解决方法既幽默又乐于助人。
    【解决方案3】:

    错字。将“模板”更改为“模板”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-23
      • 2012-08-23
      • 2016-01-01
      • 1970-01-01
      • 2013-03-02
      相关资源
      最近更新 更多