模板不存在于/博客/索引.htm

2024-10-01 15:39:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Django版本1.9.x创建我自己的项目,但是我无法使位于/的索引页正常工作。在

这是我的文件夹结构:

rxe/
    urls.py
    wsgi.py
    settings.py
blog/
    migrations/
    static/
        blog/
            css/
            js/
            img/
    templates/
        blog/
           index.html
    admin.py
    models.py
    tests.py
    urls.py
    views.py
manage.py

rxe/urls.py

^{pr2}$

blog/urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name = 'index'),
]

blog/views.py

from django.http import HttpResponse, HttpResponseRedirect
from django.views.generic import TemplateView

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

访问localhost:8000/时,错误:

TemplateDoesNotExist at /

blog/index.html

在它的正下方,在模板加载器后期分析中

django.template.loaders.app_directories.Loader: /usr/local/lib/python2.7/dist-packages/Django-1.9.4-py2.7.egg/django/contrib/admin/templates/blog/index.html (Source does not exist)

django.template.loaders.app_directories.Loader: /usr/local/lib/python2.7/dist-packages/Django-1.9.4-py2.7.egg/django/contrib/auth/templates/blog/index.html (Source does not exist)

如果我返回一个HttpResponse('Hello'),就像在教程中一样,它是有效的,但是这个教程没有/路径的任何视图,我想要一个。在

编辑

rxe/settings.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',
        ],
    },
    },
]

Tags: djangofrompyimportindexhtmlcontexttemplate
1条回答
网友
1楼 · 发布于 2024-10-01 15:39:41

您必须将blog应用程序添加到项目中,方法是将它包含在INSTALLED_APPS设置中:

INSTALLED_APPS = [...,
                  'blog']

否则,模板加载器将不会处理blog目录,因为它不被视为应用程序。在

相关问题 更多 >

    热门问题