首页 文章

Django - 教程 - 设置IndexView但获取TemplateDoesNotExist

提问于
浏览
0

我已经在TemplateDoesNotExist上搜索了所有问题 . 我也在进行大约12个小时的故障排除 . 有一些我不理解的东西 . 我是django的新手 .

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

这是错误:

Exception Location: C:\ Users \ Workstation333 \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ django \ template \ loader.py in select_template,第47行

Exception Type: TemplateDoesNotExist

Exception Value: index.html

模板加载器postmortem

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')
]

部分settings.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',
            ],
        },
    },
]

请帮帮忙,谢谢 .

编辑:

我在代码中添加了以下内容,并且我没有立即打破它,但我不认为这是一个解决方案,而是一个破解或无法维护的黑客攻击 . 我在Dirs中添加了admin索引的索引,如下所示:

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

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

1 回答

  • 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

相关问题