**我正在使用Django 1.8。在此版本的Django中,模板功能已更改。在這裏閱讀更多Upgrading templates to Django 1.8 **從Django獲取TemplateDoesNotExist 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 /模板:
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, '模板')' – Pynchia
在這裏看到:http://stackoverflow.com/questions/4562252/django-how-to-deal-the-paths-in-settings-py-on-collaborative-projects – Pynchia
你應該補充說,作爲一個答案@Pynchia –