今天在學到Django類視圖的時候,訪問子路由,提示:TemplateDoesNotExist at /index/,具體內容截圖如下:
子應用childapp的視圖文件內容已經項目目錄如下方截圖所示:
test_django下settings.py (至於爲什麼看該py文件,大夥看了解下Django項目下不同文件作用)目錄下TEMPLATES內容如下:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
原因分析: 報錯TemplateDoesNotExist at /index/ ,那我們就找看看是不是Template配置的問題,我們發現DIRS是[], 那麼自然,Template只會檢查django\template目錄下是否是存在template_name, 所以,我們需要將自己定義的templetes目錄添加進來(注意,templates在項目下的子目錄),代碼如下:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'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',
],
},
},
]
此時問題就完美解決了,可以正常訪問了