首页 > 解决方案 > TemplateDoesNotExist at / 错误,即使 index.html 存在于所述文件夹中

问题描述

跟进 [ https://realpython.com/location-based-app-with-geodjango-tutorial/]并实施附近的商店应用程序。但是,即使文件存在于指定的文件夹中,也会在 / 处获得 TemplateDoesNotExist。

尝试过的解决方案:

  1. 在“商店”应用程序中创建子目录(模板)

  2. 在 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)

` 此外,我不在虚拟环境中工作(如果这有影响,尽管它不应该)

标签: djangoviewpython-3.6

解决方案


您可以尝试将“DIRS”替换为:

'DIRS': [os.path.join(BASE_DIR, 'templates')],

不要忘记在设置文件的同一路径中拥有 /templates 文件夹。


推荐阅读