django - TemplateDoesNotExist at / 错误,即使 index.html 存在于所述文件夹中
问题描述
跟进 [ https://realpython.com/location-based-app-with-geodjango-tutorial/]并实施附近的商店应用程序。但是,即使文件存在于指定的文件夹中,也会在 / 处获得 TemplateDoesNotExist。
尝试过的解决方案:
在“商店”应用程序中创建子目录(模板)
在 settings.py 中更改了 DIRS
设置.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [''],
'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',
],
},
},
]
此外,模板事后分析指出:
模板加载器事后分析
Django 尝试按以下顺序加载这些模板:
使用引擎 django:
django.template.loaders.filesystem.Loader: /home/username/sampleShops/nearbyshops/nearbyshops/shops (Source does not exist)
django.template.loaders.app_directories.Loader: /home/username/.local/lib/python3.6/site-packages/django/contrib/admin/templates/nearbyshops/shops (Source does not exist)
django.template.loaders.app_directories.Loader: /home/username/.local/lib/python3.6/site-packages/django/contrib/auth/templates/nearbyshops/shops (Source does not exist)
django.template.loaders.app_directories.Loader: /home/username/.local/lib/python3.6/site-packages/django/contrib/gis/templates/nearbyshops/shops (Source does not exist)
` 此外,我不在虚拟环境中工作(如果这有影响,尽管它不应该)
解决方案
您可以尝试将“DIRS”替换为:
'DIRS': [os.path.join(BASE_DIR, 'templates')],
不要忘记在设置文件的同一路径中拥有 /templates 文件夹。
推荐阅读
- spring-boot - spring-kafka 中的消费者错误处理中的重试尝试计数不起作用
- python - 使用 pyomo 模型优化功能
- python - 为什么我的 python 代码不输出浮点数?
- kubernetes - Jenkins-X 3 operator install 无法下载“jx3/jx-git-operator”
- vb.net - 转义表示 sql 查询的字符串中的双引号字符
- javascript - 在云函数中,写入 Firestore 子集合使用什么语法?
- javascript - 将 title 属性用于带有 fancybox4 的标题
- ruby-on-rails - 如何使用流程图构建自动化规则?
- typescript - 为什么我没有得到我的泛型的属性?
- python - Pandas Datareader 到 csv 文件结构交换