settings.py中的template文件夹的Django路径不正确 [英] Incorrect Django path to templates folder in settings.py

查看:53
本文介绍了settings.py中的template文件夹的Django路径不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习Django,并遵循了 Learn Django 1.11 教程.

I am newly learning Django and was following the Learn Django 1.11 Tutorial.

这是我当前的项目树:

├── 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

我正在尝试将路径添加到settings文件夹中的tempelates文件夹.但是显示的错误

I am trying to add the path to the tempelates folder in the settings folder. But the error shown

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

django.template.loaders.filesystem.Loader: .../muypicky/templates/index.html (Source does not exist)

当前setting.py文件

Current setting.py file

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

看着错误,文件路径错误,因为它进入/muypicky/tempelates,这是不正确的.因此,如何在setting.py(base.py)中使用给定的文件树进入根文件夹,然后进入tempelates文件夹.

Looking at the error, the file path is wrong because it goes into /muypicky/tempelates which is incorrect. So how do I get to root folder and then into tempelates folder with the given file tree in setting.py (base.py).

任何进一步的查询,只是问一下,期待着大家的感谢.

Any further queries, just ask and many thanks in anticipation.

推荐答案

解决方案:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

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

包装了 os.path.dirname()后可返回一个文件夹

Addition of os.path.dirname() was wrapped to go back one folder

这篇关于settings.py中的template文件夹的Django路径不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆