Django再也找不到模板(TemplateDoesNotExist)

时间:2015-04-08 08:36:53

标签: django django-templates

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

就像我说的,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之间的冲突

2 个答案:

答案 0 :(得分: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',
            ],
        },
    },
]

答案 1 :(得分:0)

请仔细检查您的模板文件路径和文件名。也许你可以提供你的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