Django TemplateDoesNotExist: A Quick Fix

There are several reasons why you might see the “TemplateDoesNotExist” error in Django:

  1. The template file is not located in one of the directories specified in the TEMPLATES setting in your Django settings file.
  2. The template file is not in the templates folder in an installed app
  3. The APP_DIRS settings is set to False
  4. The template file is located in the correct directory, but the filename is spelled incorrectly or the file extension is incorrect.

How to Fix the TemplateDoesNotExist Error

Here are some steps you can take to troubleshoot and fix the “TemplateDoesNotExist” error:

Check the TEMPLATES Setting

The first thing you should check is the TEMPLATES setting in your Django settings file. Make sure that the directories specified in this setting contain the template file you are trying to use.

If the template file is not in the templates folder of one of the installed apps, then you must manually add the directory to the DIRS list.

For example, by default the DIRS list is empty:

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', ], }, }, ]
Code language: Python (python)

But you can add your directory like this:

TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [ '/path/to/new/template/directory', ], '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', ], }, }, ]
Code language: Python (python)

This only needs to be done if you did not add it to the templates folder of an application. Keep reading if that was the case.

The APP_DIRS Settings

By default, the APP_DIRS setting in the TEMPLATES setting is set to True, which means that Django will search for templates in a templates folder within each installed app. So, if you are experiencing this error then you should also check that the APP_DIRS setting has not been changed to False.

Also, make sure that the application that the template file is in has been added to INSTALLED_APPS.

For reference, here is the default value of the TEMPLATES setting (for Django 4.1):

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', ], }, }, ]
Code language: Python (python)

Check the Filename and Extension

If the template file is located in the correct directory, check the spelling of the filename and the extension. The filename and extension must match exactly what you are trying to render in your Django view.

Conclusion

The “TemplateDoesNotExist” error in Django can be frustrating, but it is usually caused by a problem with the location, spelling, or syntax of the template file. By following the steps outlined in this article, you should be able to quickly troubleshoot and fix this error.

References


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *