【问题标题】:Django - Tutorial - Setting IndexView but Getting TemplateDoesNotExistDjango - 教程 - 设置 IndexView 但获取 TemplateDoesNotExist
【发布时间】:2018-10-25 19:34:53
【问题描述】:

我已经在 TemplateDoesNotExist 上搜索了所有问题。 我也已经对此进行了大约 12 个小时的故障排除。 有一点我不明白。我是 Django 新手。

我正在关注以下 Thinkster 教程: Building Web Applications with Django and AngularJS

这是错误:

异常位置: C:\Users\Workstation333\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\loader.py 在 select_template 第 47 行

异常类型: TemplateDoesNotExist

异常值: index.html

模板加载器事后分析

Django 尝试按以下顺序加载这些模板: 使用引擎django
  • django.template.loaders.app_directories.Loader: C:\Users\Workstation333\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\admin\templates\index.html(源不存在)
  • django.template.loaders.app_directories.Loader: C:\Users\Workstation333\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\templates\index.html(源不存在)
  • django.template.loaders.app_directories.Loader: C:\Users\Workstation333\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\templates\index.html(源不存在)

这个 IndexView 代码我不明白,我相信问题出在哪里。本教程不会详细介绍此代码。我认为这是改变 index.html 或如何找到它的原因。这是在我的views.py 中,并且是我的urls.py 所在的同一个项目文件夹。

views.py

from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.generic.base import TemplateView
from django.utils.decorators import method_decorator

class IndexView(TemplateView):
    template_name = 'index.html'

    @method_decorator(ensure_csrf_cookie)
    def dispatch(self, *args, **kwargs):
        return super(IndexView, self).dispatch(*args, **kwargs)

url.py

from django.contrib import admin
from django.urls import path, re_path, include
from rest_framework_nested import routers
from authentication.views import AccountViewSet
from HawkProject.views import IndexView

router = routers.SimpleRouter()
router.register(r'accounts', AccountViewSet)

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path(r'^api/v1/', include(router.urls)),

    re_path(r'^.*$', IndexView.as_view(), name='index')
]

部分设置.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

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',
            ],
        },
    },
]

请帮忙,谢谢。

编辑:

我在代码中添加了以下内容,并且我让它不会立即中断, 但我不认为这是一个解决方案,而是一个会破坏或不可维护的 hack。我在 Dirs 中添加了管理索引的索引,如下所示:

 'DIRS': 
['C:/Users/Workstation333/AppData/Local/Programs/Python/Python36/Lib/site- 
packages/django/contrib/admin/templates/admin'],

我在它认为应该存在的目录中找不到其他索引文件。 *注意上面目录中的双重管理员,不知道为什么。它还使“主页”页面直接将我带到管理页面。

【问题讨论】:

  • 嗯,你有 index.html 吗?它在哪里?
  • 我相信是这个:C:\Users\Workstation333\Documents\Git\Hawk\HawkProject\templates\index.html

标签: django angular


【解决方案1】:

我没有通读你正在做的教程,但尝试这样做:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    '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',
        ],
    },
},

]

如您所见,我认为您收到此错误是因为您的模板目录 'DIRS' 是空白的,因此 Django 不知道在哪里搜索 'index.html''templates' 名称可以更改为您想要的任何名称,但只需根据您在每个应用程序中保存模板的文件夹命名即可。

Django Templates

【讨论】:

  • 我照你说的做了,但我得到了另一个可能不相关的错误,关于 compress is not a registered tag library。所以这可能是解决方案,但我需要确定。
  • 当您尝试从尚未在 settings.py 文件的 INSTALLED_APPS 部分列出的应用程序中加载某些内容时,通常会发生这种情况。我猜本教程希望您向已安装的应用程序添加一些内容。尝试再次检查教程。但是,是的,我认为这个错误是无关的。
  • 是的,这无关。谢谢。
猜你喜欢
  • 1970-01-01
  • 2014-07-05
  • 2021-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-13
相关资源
最近更新 更多