'Django Exception: 'TemplateDoesNotExist at /'

I'm new to Django and trying to convert a HTML template to Django project.

This is my directory structure:

.
└── MyProject
    ├── db.sqlite3
    ├── main
    │   ├── admin.py
    │   ├── apps.py
    │   ├── __init__.py
    │   ├── migrations
    │   │   ├── __init__.py
    │   │   └── __pycache__
    │   │       └── __init__.cpython-38.pyc
    │   ├── models.py
    │   ├── __pycache__
    │   │   ├── admin.cpython-38.pyc
    │   │   ├── apps.cpython-38.pyc
    │   │   ├── __init__.cpython-38.pyc
    │   │   ├── models.cpython-38.pyc
    │   │   ├── urls.cpython-38.pyc
    │   │   └── views.cpython-38.pyc
    │   ├── static
    │   │   ├── assets
    │   │   │   ├── css
    │   │   │   │   ├── bootstrap.css
    │   │   │   │   ├── framework.css
    │   │   │   │   ├── framework-rtl.css
    │   │   │   │   ├── icons.css
    │   │   │   │   ├── night-mode.css
    │   │   │   │   ├── style.css
    │   │   │   │   └── style-rtl.css
    │   │   │   ├── images
    │   │   │   │   ├── 1920x1080
    │   │   │   │   │   ├── img1.html
    │   │   │   │   │   ├── img2.html
    │   │   │   │   │   └── img3.html
    │   │   │   │   ├── avatars
    │   │   │   │   │   ├── avatar-1.jpg
    │   │   │   │   │   ├── avatar-2.jpg
    │   │   │   │   │   ├── avatar-3.jpg
    │   │   │   │   │   ├── avatar-4.jpg
    │   │   │   │   │   └── home-profile.jpg
    │   │   │   │   ├── bg-shapes
    │   │   │   │   │   ├── course-dhashboard-bg-dark.jpg
    │   │   │   │   │   ├── course-dhashboard-bg-dark-rtl.jpg
    │   │   │   │   │   ├── course-dhashboard-bg.jpg
    │   │   │   │   │   ├── course-dhashboard-bg-rtl.jpg
    │   │   │   │   │   ├── tab-border-left-dark.png
    │   │   │   │   │   ├── tab-border-left.png
    │   │   │   │   │   ├── tab-border-right-dark.png
    │   │   │   │   │   └── tab-border-right.png
    │   │   │   │   ├── blog
    │   │   │   │   │   ├── img-1.jpg
    │   │   │   │   │   ├── img-2.jpg
    │   │   │   │   │   ├── img-3.jpg
    │   │   │   │   │   ├── img-4.jpg
    │   │   │   │   │   └── img-8.jpg
    │   │   │   │   ├── book
    │   │   │   │   │   ├── css3.jpg
    │   │   │   │   │   ├── html5.jpg
    │   │   │   │   │   ├── php.jpg
    │   │   │   │   │   ├── vue-2-basics-.jpg
    │   │   │   │   │   └── win8.jpg
    │   │   │   │   ├── course
    │   │   │   │   │   ├── 1.png
    │   │   │   │   │   ├── 2.png
    │   │   │   │   │   ├── 3.png
    │   │   │   │   │   ├── 4.png
    │   │   │   │   │   ├── 5.png
    │   │   │   │   │   ├── 6.png
    │   │   │   │   │   ├── 7.png
    │   │   │   │   │   ├── 9.png
    │   │   │   │   │   ├── p-1.png
    │   │   │   │   │   ├── p-2.png
    │   │   │   │   │   └── p-3.png
    │   │   │   │   ├── demos
    │   │   │   │   │   ├── elements
    │   │   │   │   │   │   ├── dark.jpg
    │   │   │   │   │   │   ├── image.jpg
    │   │   │   │   │   │   ├── light.jpg
    │   │   │   │   │   │   ├── slider1.jpg
    │   │   │   │   │   │   ├── slider2.jpg
    │   │   │   │   │   │   ├── slider3.jpg
    │   │   │   │   │   │   └── slider4.jpg
    │   │   │   │   │   ├── feather-icons.jpg
    │   │   │   │   │   ├── fontawesome-icons.jpg
    │   │   │   │   │   ├── line-awesome-icons.jpg
    │   │   │   │   │   └── material-icons.jpg
    │   │   │   │   ├── favicon.png
    │   │   │   │   ├── feature-2.png
    │   │   │   │   └── feature.png
    │   │   │   └── js
    │   │   │       ├── bootstrap-select.min.js
    │   │   │       ├── framework.js
    │   │   │       ├── jquery-3.3.1.min.js
    │   │   │       ├── main.js
    │   │   │       ├── mmenu.min.js
    │   │   │       └── simplebar.js
    │   │   ├── css
    │   │   │   ├── css-bootstrap.css
    │   │   │   ├── css-framework-rtl.css
    │   │   │   ├── css-icons.css
    │   │   │   ├── css-night-mode.css
    │   │   │   ├── css-style-rtl.css
    │   │   │   └── htdocs_error-style.css
    │   │   ├── favicons
    │   │   │   └── images-favicon.png
    │   │   ├── fonts
    │   │   │   ├── fonts-Brand-Icons.svg
    │   │   │   ├── fonts-Brand-Icons.ttf
    │   │   │   ├── fonts-Brand-Icons.woff
    │   │   │   ├── fonts-Feather-Icons.svg
    │   │   │   ├── fonts-Feather-Icons.ttf
    │   │   │   ├── fonts-Feather-Icons.woff
    │   │   │   ├── fonts-Gotham-Rounded-Bold.ttf
    │   │   │   ├── fonts-Gotham-Rounded-Medium.ttf
    │   │   │   ├── fonts-Gotham-Rounded.ttf
    │   │   │   ├── fonts-Line-Awesome.svg
    │   │   │   ├── fonts-Line-Awesome.ttf
    │   │   │   ├── fonts-Line-Awesome.woff
    │   │   │   ├── fonts-MaterialIcons-Regular.eot
    │   │   │   ├── fonts-MaterialIcons-Regular.ttf
    │   │   │   ├── fonts-MaterialIcons-Regular.woff
    │   │   │   ├── fonts-MaterialIcons-Regular.woff2
    │   │   │   ├── fonts-Material-Icons.svg
    │   │   │   ├── fonts-Material-Icons.ttf
    │   │   │   ├── fonts-Material-Icons.woff
    │   │   │   ├── fonts-unicons.eot
    │   │   │   ├── fonts-unicons.svg
    │   │   │   ├── fonts-unicons.ttf
    │   │   │   ├── fonts-unicons.woff
    │   │   │   └── images-icon-play.svg
    │   │   ├── images
    │   │   │   ├── avatars-avatar-1.jpg
    │   │   │   ├── avatars-avatar-2.jpg
    │   │   │   ├── avatars-avatar-3.jpg
    │   │   │   ├── avatars-avatar-4.jpg
    │   │   │   ├── avatars-home-profile.jpg
    │   │   │   ├── bg-shapes-course-dhashboard-bg-dark-rtl.jpg
    │   │   │   ├── bg-shapes-course-dhashboard-bg-rtl.jpg
    │   │   │   ├── bg-shapes-tab-border-left-dark.png
    │   │   │   ├── bg-shapes-tab-border-left.png
    │   │   │   ├── bg-shapes-tab-border-right-dark.png
    │   │   │   ├── bg-shapes-tab-border-right.png
    │   │   │   ├── book-css3.jpg
    │   │   │   ├── book-html5.jpg
    │   │   │   ├── book-php.jpg
    │   │   │   ├── book-vue-2-basics-.jpg
    │   │   │   ├── book-win8.jpg
    │   │   │   ├── course-1.png
    │   │   │   ├── course-2.png
    │   │   │   ├── course-3.png
    │   │   │   ├── course-4.png
    │   │   │   ├── course-5.png
    │   │   │   ├── course-6.png
    │   │   │   ├── course-7.png
    │   │   │   ├── course-9.png
    │   │   │   ├── course-p-1.png
    │   │   │   ├── course-p-2.png
    │   │   │   ├── course-p-3.png
    │   │   │   ├── demos-feather-icons.jpg
    │   │   │   ├── demos-fontawesome-icons.jpg
    │   │   │   ├── demos-line-awesome-icons.jpg
    │   │   │   ├── demos-material-icons.jpg
    │   │   │   ├── elements-dark.jpg
    │   │   │   ├── elements-image.jpg
    │   │   │   ├── elements-light.jpg
    │   │   │   ├── episodes-1.png
    │   │   │   ├── episodes-2.png
    │   │   │   ├── episodes-3.png
    │   │   │   ├── episodes-5.png
    │   │   │   ├── htdocs_error-something-lost.png
    │   │   │   └── images-logo-dark.svg
    │   │   ├── js
    │   │   │   ├── js-bootstrap-select.min.js
    │   │   │   ├── js-framework.js
    │   │   │   ├── js-jquery-3.3.1.min.js
    │   │   │   ├── js-main.js
    │   │   │   ├── js-simplebar.js
    │   │   │   ├── package.json
    │   │   │   └── query.js
    │   │   └── server.js
    │   ├── tests.py
    │   ├── urls.py
    │   └── views.py
    ├── manage.py
    ├── technocolabs
    │   ├── asgi.py
    │   ├── __init__.py
    │   ├── __pycache__
    │   │   ├── __init__.cpython-38.pyc
    │   │   ├── settings.cpython-38.pyc
    │   │   ├── urls.cpython-38.pyc
    │   │   └── wsgi.cpython-38.pyc
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    └── templates
        └── main
            └── index.html

I have placed my html templates in 'MyProjects/templates/main'. but every time I run server, I get following exception:

TemplateDoesNotExist at /
main/index.html
Request Method: GET
Request URL:    http://localhost:8000/
Django Version: 3.2.4
Exception Type: TemplateDoesNotExist
Exception Value:    
main/index.html
Exception Location: /home/wiz/.virtualenvs/django3/lib/python3.8/site-packages/django/template/loader.py, line 19, in get_template
Python Executable:  /home/wiz/.virtualenvs/django3/bin/python3
Python Version: 3.8.5
Python Path:    
['/home/wiz/virtualenv/technocolabs',
 '/usr/lib/python38.zip',
 '/usr/lib/python3.8',
 '/usr/lib/python3.8/lib-dynload',
 '/home/wiz/.virtualenvs/django3/lib/python3.8/site-packages']
Server time:    Sat, 12 Jun 2021 10:31:45 +0000

For reference here are the contents of 'MyProject/main/views.py';

from django.shortcuts import render

def index (request): 
    return render(request, 'main/index.html')

and the contents of settings.py

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-!6s6741@g3)2llf4h!(4y5bk%_2ts$m)vt7!in#v92!^&=0n-x'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main.apps.MainConfig',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'technocolabs.urls'

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',
            ],
        },
    },
]

WSGI_APPLICATION = 'technocolabs.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = '/static/'

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

I was going through the following tutorial: Converting any HTML template into a Django template

I have no idea what is it that I'm doing wrong.



Solution 1:[1]

Your TEMPLATES setting is as follows (truncated to keep answer short):

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        ...
    },
]

The problem here is that the DIRS part is just an empty list. By default Django will search all the directories in DIRS for templates and if APP_DIRS is True it will also search the sub-directory templates in each of the apps present in INSTALLED_APPS. You need to add your directory to the DIRS list. Modify the setting as so:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'], # Add templates directory to the list
        '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',
            ],
        },
    },
]

Solution 2:[2]

You just have to change the location of templates. It's supposed to be inside the app, main, instead of inside the project.

Solution 3:[3]

First: Import os

import os

Second: Replace the following line:

BASE_DIR = Path(__file__).resolve().parent.parent

With this :

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

Third: Replace The Database Code :

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': BASE_DIR / 'db.sqlite3',
} }

With This Code:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}}

Fouth: Make sure that your templates folder in the app directory for example projectfoldername/appfoldername/templates/appfoldername/index.html

fifth: Make sure when using a function in views.py write it like this for example

def index(request):
return render(request,"newapp/index.html")

Solution 4:[4]

I had a similar issue. My template directory was correct but I still got template doesn't exist error. I solved the problem by renaming my HTML file from index.html to based.html. my point is ' I guess the word index was the problem, you could try renaming yours too.'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdul Aziz Barkat
Solution 2 EE2021
Solution 3 MoeDevo
Solution 4 NWEKE LESLEY