settings.py 中模板文件夹的 Django 路径

问题描述 投票:0回答:4

我刚刚学习 Django,并且正在学习 学习 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(源不存在)

当前设置.py文件

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

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

如有任何疑问,请询问,非常感谢。

python django
4个回答
4
投票

我有相同的项目树,并在模板中放置:

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',
        ],
    },
},

]


4
投票

您可能拼错了名称,因为该目录名为 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(来源不存在)


2
投票

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


0
投票

您的 Index.html 文件实际上不在 muypicky/templates/index.html 在里面 当前项目/index.html 所以你需要将该index.html文件移动到你的 多挑剔/模板/位置或

您需要将其保留在 BASE_Dir 文件夹设置中

© www.soinside.com 2019 - 2024. All rights reserved.