首页 > 解决方案 > 如何修复 Windows10 上的 TemplateDoesNotExist 错误?

问题描述

我一直在关注[编写您的第一个 Django 应用程序][1] 的教程

在第 3 部分中,我尝试使用模板。我在 Windows10 上使用 Python 3.1、Django 3.2。

以下是我得到的错误:

  Django tried loading these templates, in this order:

  Using engine django:

   - django.template.loaders.app_directories.Loader: 

      C:\Users\KKK\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\admin\templates\polls\index.html (Source does not exist)

   - django.template.loaders.app_directories.Loader: 

      C:\Users\KKK\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\auth\templates\polls\index.html (Source does not exist)

下面是我的文件结构:

mysite
+-- mysite
|   +--settings.py
|   +--other files
+-- polls
|   +--templates
|   |   +--polls
|   |   |  +--index.html
|   +--views.py
|   +--other files
+-- db.sqlite3
+-- manage.py`

我在 INSTALLED_APPS 设置中添加了对 polls 配置类的引用。

INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

任何帮助,将不胜感激。

标签: djangodjango-templates

解决方案


您可能必须向 django 指示在哪里可以找到您的模板,将其(大约第 60 行)放在 settings.py

# settings.py
TEMPLATES = [
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
]

推荐阅读