我正在学习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
我正在尝试将路径添加到settings文件夹中的templates文件夹。但错误显示
django.template.loaders.filesystem.Loader:... / muypicky / templates / index.html(源不存在)
当前的setting.py文件
TEMPLATES = [{
'DIRS': [os.path.join(BASE_DIR, 'templates')],
},]
查看错误,文件路径是错误的,因为它进入/ muypicky / tempelates这是不正确的。那么如何使用setting.py(base.py)中的给定文件树到root文件夹然后进入tempelates文件夹。
任何进一步的询问,只是问及非常感谢期待。
解:
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()
被包裹回去一个文件夹
settings.朋友
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads')
MEDIA_URL = '/uploads/'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [MEDIA_ROOT + '/'],#other app templates
'APP_DIRS': True,#heremention yourapp/templates
'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'
],
},
},
]
你也可以看看djano template文件
我遇到了类似的问题,能够纠正这个问题:
TEMPLATES = [{
....
'DIRS': [os.path.join(BASE_DIR, '/templates')],
....
开发环境:django 2.2,python 3.6
所以我认为你需要在'模板'字符串之前添加/
...
我也认为这会改变django版本,因为我正在研究另一个项目,它不需要在django 2.1,python 3.6上的'templates'字符串之前跟踪/
。所以我觉得值得尝试一下好运