3. 運行Django項目,訪問某個路由地址,提示:TemplateDoesNotExist at /index/

今天在學到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',
            ],
        },
    },
]

此時問題就完美解決了,可以正常訪問了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章