Fix TemplateDoesNotExist: TemplateDoesNotExist at /dashboard/ — dashboard/index.html in Django
This error means Django cannot find the specified template file in any of its configured template directories. Fix it by verifying the TEMPLATES setting includes your app's directories, ensuring APP_DIRS is True, and checking that the template file path matches the string passed to render() exactly, including subdirectory names.
Reading the Stack Trace
Here's what each line means:
- File "/app/dashboard/views.py", line 8, in index: Your view passes 'dashboard/index.html' to render(), but Django cannot locate this template anywhere on its search path.
- File "/venv/lib/python3.11/site-packages/django/template/loader.py", line 19, in get_template: Django's template loader tried all configured backends and directories but found no matching file.
- raise TemplateDoesNotExist(template_name, chain=chain): The chain variable lists every loader that was tried, which is useful for debugging which directories were searched.
Common Causes
1. APP_DIRS not enabled
The TEMPLATES setting has APP_DIRS set to False, so Django does not search inside each app's templates/ directory.
# settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': False, # Templates inside apps are not discovered
'OPTIONS': { ... },
},
]
2. Template in wrong directory structure
The template file exists but is not inside the expected app_name/templates/app_name/ subdirectory.
# File is at dashboard/index.html instead of dashboard/templates/dashboard/index.html
# views.py
def index(request):
return render(request, 'dashboard/index.html')
3. App not in INSTALLED_APPS
The app containing the template is not registered in INSTALLED_APPS, so Django's app-directory loader skips it.
# settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
# 'dashboard', # Missing!
]
The Fix
Place the template at dashboard/templates/dashboard/index.html so Django's app-directory loader can find it. Enable APP_DIRS and add the app to INSTALLED_APPS. The double directory naming (dashboard/templates/dashboard/) is a Django convention to namespace templates.
# dashboard/views.py
def index(request):
return render(request, 'dashboard/index.html')
# Template file located at: dashboard/index.html (WRONG)
# 1. Move template to correct location:
# dashboard/templates/dashboard/index.html
# 2. Ensure app is in INSTALLED_APPS:
# settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'dashboard', # Added
]
# 3. Ensure APP_DIRS is True:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
'OPTIONS': { ... },
},
]
Testing the Fix
import pytest
from django.test import TestCase, Client
class TestDashboardTemplate(TestCase):
def setUp(self):
self.client = Client()
def test_dashboard_index_renders(self):
response = self.client.get('/dashboard/')
assert response.status_code == 200
self.assertTemplateUsed(response, 'dashboard/index.html')
def test_dashboard_index_contains_expected_content(self):
response = self.client.get('/dashboard/')
assert response.status_code == 200
self.assertContains(response, '<div')
def test_template_exists(self):
from django.template.loader import get_template
template = get_template('dashboard/index.html')
assert template is not None
Run your tests:
pytest
Pushing Through CI/CD
git checkout -b fix/template-not-found-dashboard,git add dashboard/templates/dashboard/index.html settings.py,git commit -m "fix: move template to correct directory and enable APP_DIRS",git push origin fix/template-not-found-dashboard
Your CI config should look something like this:
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
cache: 'pip'
- run: pip install -r requirements.txt
- run: python manage.py check --deploy
- run: pytest --tb=short -q
The Full Manual Process: 18 Steps
Here's every step you just went through to fix this one bug:
- Notice the error alert or see it in your monitoring tool
- Open the error dashboard and read the stack trace
- Identify the file and line number from the stack trace
- Open your IDE and navigate to the file
- Read the surrounding code to understand context
- Reproduce the error locally
- Identify the root cause
- Write the fix
- Run the test suite locally
- Fix any failing tests
- Write new tests covering the edge case
- Run the full test suite again
- Create a new git branch
- Commit and push your changes
- Open a pull request
- Wait for code review
- Merge and deploy to production
- Monitor production to confirm the error is resolved
Total time: 30-60 minutes. For one bug.
Or Let bugstack Fix It in Under 2 minutes
Every step above? bugstack does it automatically.
Step 1: Install the SDK
pip install bugstack
Step 2: Initialize
import bugstack
bugstack.init(api_key=os.environ["BUGSTACK_API_KEY"])
Step 3: There is no step 3.
bugstack handles everything from here:
- Captures the stack trace and request context
- Pulls the relevant source files from your GitHub repo
- Analyzes the error and understands the code context
- Generates a minimal, verified fix
- Runs your existing test suite
- Pushes through your CI/CD pipeline
- Deploys to production (or opens a PR for review)
Time from error to fix deployed: Under 2 minutes.
Human involvement: zero.
Try bugstack Free →No credit card. 5-minute setup. Cancel anytime.
Deploying the Fix (Manual Path)
- Run the full test suite locally to confirm the template renders.
- Open a pull request with the template relocation and settings change.
- Wait for CI checks to pass on the PR.
- Have a teammate review and approve the PR.
- Merge to main and verify the deployment in staging before promoting to production.
Frequently Asked Questions
BugStack runs the fix through your existing test suite, generates additional template-rendering tests, and validates that all views return 200 before marking it safe to deploy.
BugStack never pushes directly to production. Every fix goes through a pull request with full CI checks, so your team can review it before merging.
The dashboard/templates/dashboard/ pattern namespaces templates so that two apps with an index.html do not collide. Without it, Django may load the wrong app's template.
Yes. Add the directory to the DIRS list in TEMPLATES. For example: 'DIRS': [BASE_DIR / 'templates']. This is common for project-wide base templates.