Fixing TemplateDoesNotExist Django error

Django is a popular Python web framework. Because it is popular, a lot of people are learning Django. And if you’ve just started learning Django and are stuck with TemplateDoesNotExist error, see the solution in this article.

“TemplateDoesNotExist at /” error

So, when developing your Python Django web application, you can encounter the following error:

TemplateDoesNotExist at /

Request Method: GET
Request URL: http://localhost:8000/
Django Version: 3.2
Exception Type: TemplateDoesNotExist
Python Executable: /usr/bin/python3
Python Version: 3.9.8

Django tried loading these templates in, this order:
Using engine django:
django.template.loaders.filesystem.Loader: ...
...
Traceback:
...

The error has long traceback next to it. This error means that Django framework cannot find a template for requested URL. If you already have your templates/ and views.py trying to render them, go ahead to Fix #3. There are few possible reasons Django can issue that error. Let’s consider them below.

Fix #1: add templates

Make sure you add actual templates to your templates folder. If you don’t have any templates, create templates in the base directory of your website and add a template (for example, index.html). I will assume your app is named “app” and the project name is “website”. The structure of your project will look like this:

.
├── manage.py
├── app
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── templates
│   └── app
│       └── index.html
└── website
    ├── asgi.py
    ├── config.py
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

Fix #2: check settings, route URLs, write views

  1. Go to website/settings.py and check if your app is present in INSTALLED_APPS list. If not, add it and run python3 manage.py makemigrations; python3 manage.py migrate.
  2. Go to website/urls.py and make sure it has path('', include('app.urls')), line in the urlpatterns array.
  3. Check if app/urls.py has line from . import views at the top and path('', views.index, name='index'), in the urlpatterns array.
  4. Go to app/views.py and ensure it has index function which serves the index page. If not, write this to your app/views.py:
from django.shortcuts import render

def index(req):
    return render(req, 'app/index.html')

Next, restart your web server and check if error is gone. If not, try Fix #3.

Fix #3: specify templates directory in settings

Go to your main website/settings.py file and find the TEMPLATES array.

Add 'DIRS': [ BASE_DIR / 'templates' ], line to make the array look like this:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [ 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',
            ],
        },
    },
]

If you are wondering, “/” (slash) operator from pathlib module concatenates two Path objects properly. After the third fix, your app should finally work.

Conclusion

To recap, when you encounter TemplateDoesNotExist in Django application, you should do the following things to fix it:

  • Create templates directory with app name directory in it, which contains HTML files.
  • Check settings of your application, edit them if needed.
  • Route URLs, write page function in views.py file.
  • Add BASE_DIR/'templates' in the TEMPLATES array.
  • Restart your app.

Leave a Comment