You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django教程:配置IndexView时遇TemplateDoesNotExist错误求助

Fixing Django TemplateDoesNotError for index.html (Thinkster Tutorial)

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:

  1. Your HawkProject app isn't added to INSTALLED_APPS in settings.py
  2. You haven't created a templates directory inside your HawkProject app to store index.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 HawkProject app folder (the one containing views.py)
  • Create a new folder named templates
  • Place your index.html file inside this new templates folder

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 10:08:39