2015-09-19 81 views
0

晚上好,無法獲得TEMPLATE_DIR正確的工作路徑

我已經閱讀了多個教程的堆棧溢出的多個答案,但仍然無法正常工作。

該項目被稱爲「crepes_bretonnes」。 該項目的絕對路徑是: /Users/Jack/Desktop/Projet_Django/crepes_bretonnes

此路徑導致項目的根包含:

  • 博客
  • crepes_bretonnes
  • db.sqlite3
  • manage.py
  • static
  • 模板

settings.py是在第二個文件夾:crepes_bretonnes,包含:

import os 

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

SETTINGS_DIR = os.path.dirname(__file__) 
PROJECT_PATH = os.path.join(SETTINGS_DIR, os.pardir) 
PROJECT_PATH = os.path.abspath(PROJECT_PATH) 

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". 
    # Always use forward slashes, even on Windows. 
    # Don't forget to use absolute paths, not relative paths. 
    os.path.join(PROJECT_PATH, 'templates'), 
) 

如果我做的事情正確:Setting_Dir是Settings.py的路徑,並返回到根路徑(上一級)我使用功能pardir

所以,現在,Django會在crepes_bretonnes項目在以下路徑根目錄中搜索Templates文件夾模板: /Users/Jack/Desktop/Projet_Django/crepes_bretonnes

blog應用的views.py函數返回一個'渲染」包含模板:

return render(request, 'blog/addition.html', locals())

此模板位於在博客文件夾中的模板文件夾。在addition.html

第一行是:

{% extends "base.html" %} 

這base.html文件包含在所謂的模板的根文件夾。我不明白爲什麼它讓我給以下錯誤:

Django tried loading these templates, in this order: Using loader django.template.loaders.filesystem.Loader: Using loader django.template.loaders.app_directories.Loader: /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/Django-1.8.3-py3.4.egg/django/contrib/admin/templates/base.html (File does not exist) /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/Django-1.8.3-py3.4.egg/django/contrib/auth/templates/base.html (File does not exist) /Users/Jack/Desktop/Projet_Django/crepes_bretonnes/blog/templates/base.html (File does not exist)

其實它無法找到我的項目的根目錄下的templates文件夾中的base.html。有一些關於絕對路徑的關鍵是我必須在某個時候失蹤。

你能幫忙嗎?

編輯:我正在使用Mac OS X. 以下是設置中所請求的所有打印路徑。潘岳:

**Base_Dir** /Users/Jack/Desktop/Projet_Django/crepes_bretonnes 

**Settings_Dir** /Users/Jack/Desktop/Projet_Django/crepes_bretonnes/crepes_bretonnes 

**Project_Path** /Users/Jack/Desktop/Projet_Django/crepes_bretonnes 

**Templates_Dir** /Users/Jack/Desktop/Projet_Django/crepes_bretonnes/templates 

編輯:ANSWER

修改settings.py,以使它看起來像這樣:

import os 

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

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

您使用的是Windows嗎? os.path.join(PROJECT_PATH,'templates')'的結果是什麼? –

回答