python - 从 Django 1.8 获取 TemplateDoesNotExist

标签 python django

** 我正在使用 Django 1.8。在此版本的 Django 中,模板功能发生了变化。在这里阅读更多 Upgrading templates to Django 1.8 **

这让我很困扰,因为我遇到过这个问题并为我的其他项目之一修复了它,但这次我无法弄清楚如何解决它。我经历了无数个 stackoverflow 问题,并尝试使用我没有运气提供的答案来解决问题。这是我收到的错误消息:

Exception Type: TemplateDoesNotExist
Exception Value:    
index.html
Exception Location: /Library/Python/2.7/site-packages/django/template/loader.py in get_template, line 46
Python Executable:  /usr/bin/python
Python Version: 2.7.6
Python Path:    
['/Users/User1/Documents/PyCharmProjects/Project1',

看来是不是找错了文件夹,根据我的settings.py文件应该是在Project1/templates下找:

import os

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

TEMPLATE_PATH = os.path.join(BASE_DIR, '/templates/')

TEMPLATE_DIRS = (
    TEMPLATE_PATH,
)

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    #'django.template.loaders.eggs.load_template_source',
)

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'Project1.urls'

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

我的模板文件夹位于项目的根文件夹中。这里有什么问题?我给了它一个 TEMPLATE_DIRS 参数,并使用了一个适当的 BASE_DIR,这是大多数答案推荐的。

最佳答案

删除斜杠:TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')

参见 here

Django 1.8 的情况发生了变化,模板系统得到了改进。请参阅 release notes 。 在你的 settings.py 添加:

TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [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',
                ],
            },
        },
    ]

上面的代码直接来 self 的一个项目。 随意使用 os.path.join(BASE_DIR, 'templates') 而不是连接字符串。

关于python - 从 Django 1.8 获取 TemplateDoesNotExist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29704686/

相关文章:

python - Django URL 中的 Slug

php - Python 获取 Index Position 比 PHP 慢

python - 为什么 python 中的列表会这样?

python - 如何在openerp序列中的_interpolation_dict中创建新属性?

python - 如何添加关于夏令时时区的每周时间增量

jquery - django:发送 POST 以从可能无限的表单中查看的正确方法

Django ORM : Filter on timedelta of Datetime fields

python - 如何使用 django 中的模板过滤器计算列表中的出现次数并将其传递给模板

python - 将值捕捉到某些网格的高效 pythonic 方法

python - 连接到数据库后执行原始 SQL