一般情况下,templates路径都设置为project/templates,然后通过修改settings.py文件来声明此路径。
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], #BASE_DIR就是项目的根路径
'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',
],
},
},
]
由于不清楚os.path.join的使用细节,我将[os.path.join(BASE_DIR, 'templates')]
写成了[os.path.join(BASE_DIR, '/templates')]
这就会导致结果由BASE_DIR/templates变成了/templates,因此就会报下面的错误,显示找不到文件路径。
下面来看看带/
与不带/
的区别