Django教程:配置IndexView时遇TemplateDoesNotExist错误求助
Hey there, I know how frustrating it is to spend 12 hours troubleshooting this—let's get this sorted out quickly. The root of your problem is that Django isn't looking in the right place for your index.html template, and here's how to fix it:
Why This Happens
Looking at your error logs, Django is only checking the templates directories of built-in apps (admin, auth) and rest_framework—it's not checking your own app (HawkProject) for the template. This usually happens for one of two reasons:
- Your
HawkProjectapp isn't added toINSTALLED_APPSinsettings.py - You haven't created a
templatesdirectory inside yourHawkProjectapp to storeindex.html
Step-by-Step Solutions
1. Make Sure Your App is Registered in INSTALLED_APPS
First, open your full settings.py file and find the INSTALLED_APPS list. You need to add HawkProject (and your authentication app, since you're using it) to this list. It should look like this:
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', 'authentication', # Add your authentication app here 'HawkProject', # Add your main app here—critical! ]
When APP_DIRS = True in your TEMPLATES config, Django only checks templates directories inside apps listed in INSTALLED_APPS.
2. Create the Templates Directory in Your App
Next, set up the correct directory structure for your template:
- Navigate to your
HawkProjectapp folder (the one containingviews.py) - Create a new folder named
templates - Place your
index.htmlfile inside this newtemplatesfolder
Your project structure should now look like this:
YourProjectRoot/ HawkProject/ templates/ index.html views.py urls.py ... authentication/ manage.py settings.py ...
3. (Alternative) Use a Project-Wide Templates Directory
If you prefer to store templates in a central directory at the project root instead of per-app, modify the DIRS setting in TEMPLATES to point to a project-level templates folder:
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], # Add this line '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', ], }, }, ]
Then create a templates folder in your project root (next to manage.py) and place index.html there.
Why Your Previous Fix Wasn't Ideal
When you pointed DIRS to the admin template directory, you were telling Django to use the admin's templates instead of your own—hence why it redirected to the admin page. The fixes above will make Django look for your custom index.html in the right location.
After making these changes, restart your Django server and the TemplateDoesNotExist error should be gone!
内容的提问来源于stack exchange,提问作者everydaymage

