【问题标题】:And again Django can't find template (TemplateDoesNotExist)再次 Django 找不到模板 (TemplateDoesNotExist)
【发布时间】:2015-06-13 02:57:10
【问题描述】:

我发现很多主题都有这样的问题,但无法解决我的问题......所以再次,请帮助解决它。

就像我说的,Django 找不到模板,但它存在于“模板”目录中。而且那个目录也存在于我的“settings.py”中。

TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
#BASE_DIR + "/templates/",
)

如您所见,我尝试了两种不同的方法。没有成功...使用该目录中的所有模板。

如我所见,错误日志中没有指向我的模板文件夹的路径。

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:
    /Users/slavaspirit/Documents/Django Tests/Test05/testapp/lib/python2.7/site-packages/django/contrib/admin/templates/profile.html (File does not exist)
    /Users/slavaspirit/Documents/Django Tests/Test05/testapp/lib/python2.7/site-packages/django/contrib/auth/templates/profile.html (File does not exist)
    /Users/slavaspirit/Documents/Django Tests/Test05/testapp/lib/python2.7/site-packages/imagekit/templates/profile.html (File does not exist)
    /Users/slavaspirit/Documents/Django Tests/Test05/testapp/lib/python2.7/site-packages/allauth/templates/profile.html (File does not exist)
    /Users/slavaspirit/Documents/Django Tests/Test05/testapp/lib/python2.7/site-packages/allauth/socialaccount/providers/facebook/templates/profile.html (File does not exist)

完整设置列表:

"""
Django settings for sisrosta project.

Generated by 'django-admin startproject' using Django 1.8.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

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


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '-7g1@q9b&9ja@&xtuc%q7rk3(uon6z$lb2jr!9jcq84h2_c*fe'

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

ALLOWED_HOSTS = []

SITE_ID = 1

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'imagekit',
    'allauth',
    'taggit',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.facebook',
    'article',
    'usercard',
)

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
)

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

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.core.context_processors.tz',
    'django.contrib.messages.context_processors.messages',
    'django.core.context_processors.request',
    'allauth.account.context_processors.account',
    'allauth.socialaccount.context_processors.socialaccount',
)

LOGIN_REDIRECT_URL = '/'

SOCIALACCOUNT_QUERY_EMAIL = True

SOCIALACCOUNT_PROVIDERS = {
    'facebook': {
        'SCOPE': ['email'],
        'AUTH_PARAMS': {'auth_type': 'reauthenticate'},
        'METHOD': 'oauth2',
        'VERIFIED_EMAIL': False
    }
}

ROOT_URLCONF = 'sisrosta.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',
            ],
        },
    },
]

WSGI_APPLICATION = 'sisrosta.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'test.sqlite3'),
    }
}

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
    #BASE_DIR + "/templates/",
)

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'ru-RU'

TIME_ZONE = 'Europe/Moscow'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'

答案是: TEMPLATES 和 TEMPLATE_DIRS 之间的冲突

【问题讨论】:

  • ...您正在编辑什么设置文件?
  • 你是什么意思?)这是我项目文件夹中的基本“settings.py”文件...或者您需要查看该文件中的所有代码?
  • 你有 TEMPLATE_LOADERS 吗?
  • 其实我没有。但在以前的测试应用程序中,我也从未使用过它,但一切正常。也许是因为 Django 1.8(之前的测试是在 Django 1.7 上)?
  • 是的,所有 settings.py(不带 cmets)都可能会有所帮助。由于您提供的信息,似乎没有问题。并且可以尝试添加 TEMPLATE_LOADERS = ( "django.template.loaders.filesystem.Loader", "django.template.loaders.app_directories.Loader", 'django.template.loaders.eggs.Loader', )

标签: django django-templates


【解决方案1】:

来自Upgrading templates to Django 1.8

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

【讨论】:

  • 谢谢你 :) On 可以忘记这么简单的事情 :P
【解决方案2】:

请仔细检查您的模板文件路径和文件名。也许你可以提供你的 django 文件夹的文件结构。

这是我的settings.py关于模板文件夹:

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR,'templates'),
    os.path.join(BASE_DIR,'templates','OMS'),
    os.path.join(BASE_DIR,'templates','CRM'),
    os.path.join(BASE_DIR,'templates','PMS'),
    os.path.join(BASE_DIR,'templates','BPS'),
)

和我的模板结构:

base_template.html  PMS
BPS                 CRM                 OMS

【讨论】:

  • 嗨,谢谢回答。结构和我之前的测试一样。绝对一样... 最有趣的事实:我在我的 Test02 应用程序中遇到了这个问题。并且找不到答案,为什么 Django 看不到那个文件夹。之后,我用另一个 Virtualenv 创建了另一个项目,并将设置代码复制到新项目。一切正常!但在那个测试应用程序中 - 再次出现同样的问题!
  • 您的 Test02 应用程序文件夹结构是否与您创建的其他项目相同?顺便说一句,我在你的 settings.py 中找到了 TEMPLATES 和 TEMPLATE_DIR,它们有冲突吗?我不确定,我没有在我的 settings.py 中找到 TEMPLATES,我使用 django 1.7.4
  • 是的!这就是答案......正如我所见,TEMPLATES 是由 Django 1.8 创建的......因为我之前使用的是 1.7,所以我没有看到这种变化......我的错误,抱歉,再次感谢......
猜你喜欢
  • 2023-03-04
  • 2020-08-30
  • 2017-10-10
  • 2012-07-03
  • 2018-01-02
  • 2012-07-10
  • 2015-03-14
  • 2020-05-27
相关资源
最近更新 更多