How to Fix Template Not Found Error in Django
TemplateDoesNotExist error in Django happens when the template file is missing or Django can't find it in the configured TEMPLATES directories. To fix it, ensure your template file exists in the correct folder and that your DIRS setting in settings.py includes the right path.Why This Happens
This error occurs because Django looks for template files in specific folders defined in your project settings. If the template file is missing, misspelled, or placed outside these folders, Django cannot find it and raises the error.
from django.shortcuts import render def home(request): return render(request, 'home.html')
The Fix
Make sure your template file home.html is inside a folder listed in the DIRS list of your TEMPLATES setting in settings.py. For example, if your templates are in a folder named templates at your project root, add its full path to DIRS. This tells Django where to look for templates.
# settings.py import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 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', ], }, }, ] # Folder structure: # project_root/ # ├── templates/ # │ └── home.html # └── manage.py
Prevention
Always keep your templates organized in a dedicated folder and update DIRS in settings.py accordingly. Use consistent naming and folder structure. Enable APP_DIRS to let Django find templates inside app folders automatically. Use your IDE or editor to verify file paths and names to avoid typos.
Related Errors
1. TemplateSyntaxError: Happens when your template has invalid syntax. Check your template tags and filters.
2. ImproperlyConfigured: Occurs if TEMPLATES setting is missing or malformed. Verify your settings.py configuration.