Django无法定位到index.html模板文件,或模板相关配置不完整
配置settings.py中的模板路径出错,应该
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import os # 若已导入可忽略 # E:\django_xiangmu\py_web_try\py_web_try\settings.py TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], # 这里需要配置模板目录路径 'APP_DIRS': True, # 开启后会自动查找各应用下的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', ], }, }, ]
|
说明:BASE_DIR是 Django 在settings.py中默认定义的变量,指向项目根目录(包含manage.py的目录),os.path.join(BASE_DIR, 'templates')会自动拼接为E:\django_xiangmu\py_web_try\templates。