django - 如何修复 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',
]
任何帮助,将不胜感激。
解决方案
您可能必须向 django 指示在哪里可以找到您的模板,将其(大约第 60 行)放在 settings.py
# settings.py
TEMPLATES = [
'DIRS': [os.path.join(BASE_DIR, 'templates')],
]
推荐阅读
- sql - 来自两个相关表的 SQL 聚合函数返回相乘结果
- javascript - AppDelegate.m:40:5:使用未声明的标识符“FBSDKApplicationDelegate”
- php - 登录系统创建管理员/商店和访客页面
- python - 在 Python 中从具有特殊形式的 .csv 文件绘制 2D 图
- python - 如何在运行时使用 popen 获取程序的 strace 输出?
- flutter - flutter_background_service 返回错误 MissingPluginException
- r - 如何使用 R 中的正则表达式提取字符的第一次和第二次出现之间的部分?
- javascript - React -hook UseEffect 内存泄漏问题不止一个函数调用
- javascript - useState 没有更新 - nextjs
- python - 用阶乘替换列表中 3 的倍数的偶数