【发布时间】: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