0
0
DjangoDebug / FixBeginner · 4 min read

How to Fix Template Not Found Error in Django

The 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.

python
from django.shortcuts import render

def home(request):
    return render(request, 'home.html')
Output
django.template.exceptions.TemplateDoesNotExist: 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.

python
# 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
Output
Page renders correctly showing the content of home.html
🛡️

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.

Key Takeaways

Ensure your template files exist in folders listed in the TEMPLATES DIRS setting.
Use absolute paths with BASE_DIR to avoid path mistakes in settings.py.
Enable APP_DIRS to find templates inside app folders automatically.
Keep template names and folder structure consistent and typo-free.
Check related errors like TemplateSyntaxError for template issues.