TemplateDoesNotExit error in Django

Django internal server error 'Template does not exit'

TemplateDoesNotExit error: BitGeekTalks
Author_1

Ashwini Kumar

Nov. 18, 2021    Views: 161

Django is a free and open-source web application framework written in Python. It is used for rapid web development and clean, pragmatic design. It is built by experienced developers to make web development tasks easier, so we can focus on writing apps instead of developing the backend server tasks.

While going through the development of a Django web app we go through the various commands such as django-admin startproject myproject, django-admin startapp my app, etc. After performing the basic command a folder with the name will be created in your workspace inside that you will find a manage.py file.


To check whether the Django app is been created or not pass the command in your terminal as python manage.py runserver.
After the command gets executed you will find that the server is running in your localhost at post 8000. You faced the TemplateDoesNotExit error that means you have done all the above commands.


As we have created a templates folder inside the myapp folder. now the structure inside myproject directory is smiler to:


├── myproject
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── myapp
│   ├── admin.py
│   ├── apps.py
│   ├── models.py
│   ├── templates
│   │   └── your_app_name
│   │       └── my_index.html
│   ├── urls.py
│   └── views.py
├── manage.py
├── DB.sqlite3

Internal server error: TemplateDoesNotExit


TemplateDoesNotExit error occurs when the server call for the URL given by the developer to be redirected to a certain page and the render function doesn't get the template provided by the developer.


To solve this error first, go to your setting.py file which is inside of the /myproject/myproject/ folder, and add a few lines inside the setting.py file.

Steps to follow:


1. Add your app to INSTALLED_APPS.


INSTALLED_APPS = [
    ...
    'myapp',
    ...
]


2. Find this tuple inside the setiing.py.


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


3. Create a variable name TEMP_DIR


you will find a variable named as BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Now, create a variable with name TEMP_NAME = os.path.join(BASE_DIR,'myapp/templates') 


On the above TEMP_DIR change the 'myapp/templates' to your template name if it is in your app folder change 'myapp' as your app-name '/' name of the template. If the file structure is in the below form then just in place of 'myapp/templates'  put your template folder name as 'templates'.


├── myproject
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── myapp
│   ├── admin.py
│   ├── apps.py
│   ├── models.py
│   ├── urls.py
│   └── views.py
├── templates
│   │   └── your_app_name
│   │       └── my_index.html
├── manage.py
├── db.sqlite3


4. Add the TEMP_DIR in the TEMPLATES tuple.


At first, your TEMPLATES tuple was as bellow:
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',
                ],
            },
        },
    ]


In the 'DIRs':[], add your TEMP_DIR


After the change the tuple will be as follow:
TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [TEMP_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',
                ],
            },
        },
    ]

5. Now run the python manage.py runserver to check whether the problem is resolved or not.

As you have specified the right template folder path. you will not find this error again. 

Conclusion: TemplateDoesNotExit error occurs when the render function inside the views.py file doesn't get the proper path of the template folder to find the template HTML file.

Recommanded Articles
SEO by BitGeekTalks

Let's learn about SEO.

SEO is a well-known practice that are been added to a website to make Google know more about our services and know what does your website provides for a user. In simple words, we can say that SEO i

Backlinks

Backlinks and how to create backlinks

What is a Backlink?

In today's digital world data is gold and these data are been abstracted from the other websites to provide a user the content they are searchin

SEO tools

Top six SEO tools

Let see what are the various tools that are been used to improve the rank of a website to rank in search engines and get traffic over the website.

Now, we are gonna see 6 tools that are v

SEO and keywords - BitGeekTalks

What is SEO and Keywords in SEO?

SEO stands for Search engine optimization. It's a processing to give an upper hand on other websites that let the website be on the top of other websites that provide the same service by provid

Docker

Docker and docker architecture

In simple words, we can say that Docker is an open platform where a user can run, develop, deploy, share, and may more quickly and easily use the docker-provided functionality.