2015-04-17 77 views
4

**我正在使用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,這是大多數答案推薦的。

+1

嘗試刪除斜槓:'TEMPLATE_PATH = os.path.join(BASE_DIR, '模板')' – Pynchia

+0

在這裏看到:http://stackoverflow.com/questions/4562252/django-how-to-deal-the-paths-in-settings-py-on-collaborative-projects – Pynchia

+0

你應該補充說,作爲一個答案@Pynchia –

回答

10

刪除斜槓: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', 
       ], 
      }, 
     }, 
    ] 

上面的代碼,直接從我的一個項目來了。 隨意使用os.path.join(BASE_DIR, 'templates')而不是連接字符串。

+0

沒有骰子。刪除斜槓沒有做任何事情。 – farbodg

+0

我編輯了我的答案,看一看 – Pynchia

+0

不錯!這工作。看過我正在處理的另一個項目後,它使用Django 1.7,並且settings.py文件中缺少此部分!現在有道理。看起來像他們在1.8版中修改了模板功能。感謝Pynchia。 – farbodg

2

1)如果你對模板的路線是PROJECT_NAME/APP_NAME /模板/ APP_NAME

'DIRS': [os.path.join(BASE_DIR, 'app_name', 'templates', 'app_name')], 

2)如果你對模板的路線是項目_name /模板或PROJECT_NAME/APP_NAME/templates_only

'DIRS': [#leave it just empty, will work fine], 

注意:'template_only'表示templates/*。html模板內沒有任何文件夾。

0

我會嘗試把TEMPLATE_PATHDIRS

TEMPLATES =[ 
    {.... 
    'DIRS' : [TEMPLATE_PATH,], 
    ....