Django 1.8 TEMPLATE_DIRS被忽略了

Gad*_*s34 8 python django

这真让我抓狂.我做了一些奇怪的事情,似乎我的TEMPLATE_DIRS条目被忽略了.我只有一个settings.py文件,位于项目目录中,它包含:

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
    os.path.join(BASE_DIR, 'web_app/views/'),
)
Run Code Online (Sandbox Code Playgroud)

我将项目级模板放在/ templates文件夹中,然后在我的应用程序文件夹中包含不同视图类别的文件夹(例如,身份验证视图,帐户视图等).

例如,我的主索引页面视图位于web_app/views/main/views_main.py中,看起来像

from web_app.views.view_classes import AuthenticatedView, AppView


class Index(AppView):
    template_name = "main/templates/index.html"
Run Code Online (Sandbox Code Playgroud)

其中AppView只是TemplateView的扩展.这是我的问题:当我尝试访问该页面时,我得到一个TemplateDoesNotExist异常,而让我感到困惑的部分是Template-Loader Postmortem:

Template-loader postmortem

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
C:\Python34\lib\site-packages\django\contrib\admin\templates\main\templates\index.html (File does not exist)
C:\Python34\lib\site-packages\django\contrib\auth\templates\main\templates\index.html (File does not exist)
Run Code Online (Sandbox Code Playgroud)

为什么世界上没有搜索"模板"和"web_app/views"目录?我通过调试器和views_main.py中的断点检查了设置,看起来它们就在那里.有人有过类似的问题吗?谢谢.

JOS*_*Ftw 25

您使用的是什么版本的Django?TEMPLATE_DIRS1.8以来已弃用

从1.8版开始不推荐使用:设置DjangoTemplates后端的DIRS选项.

https://docs.djangoproject.com/en/1.8/ref/settings/#template-dirs

所以试试这个:

TEMPLATES = [
  {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [
        # insert your TEMPLATE_DIRS here
    ],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            # Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
            # list if you haven't customized them:
            'django.contrib.auth.context_processors.auth',
            'django.template.context_processors.debug',
            'django.template.context_processors.i18n',
            'django.template.context_processors.media',
            'django.template.context_processors.static',
            'django.template.context_processors.tz',
            'django.contrib.messages.context_processors.messages',
        ],
    },

  },
]
Run Code Online (Sandbox Code Playgroud)

以下是升级指南的链接:https://docs.djangoproject.com/en/1.8/ref/templates/upgrading/