2015-05-01 212 views
1

我盡我所能來源並解決了這個問題,但我似乎無法找到答案。TemplateDoesNotExist at/rango/

目前通過tangowithdjango教程,我目前在5.模板和靜態媒體。 http://www.tangowithdjango.com/book17/chapters/templates_static.html

試圖讓我的網站顯示其第一個模板,但我到了5.2我試圖加載我的網站,並得到這個錯誤信息之前:

TemplateDoesNotExist at /rango/ 
rango/index.html 
Request Method: GET 
Request URL: http://127.0.0.1:8000/rango/ 
Django Version: 1.8 
Exception Type: TemplateDoesNotExist 
Exception Value:rango/index.html 
Exception Location: /Library/Frameworks/Python.framework/Versions/2.7/lib 
/python2.7/site-packages/django/template/loader.py in get_template, line 46 
Python Executable: /Library/Frameworks/Python.framework/Versions/2.7 
/Resources/Python.app/Contents/MacOS/Python 
Python Version: 2.7.9 

這裏是我的settings.py文件:

import os 

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

TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates') 
# Template from tangowithdjango 5.1: Templates and Static Media 
# 

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. 
TEMPLATE_PATH, 
) 

這裏是我的views.py:

from django.shortcuts import render 
from django.http import HttpResponse 

def index(request): 

# Construct a dictionary to pass to the template engine as its context. 
# Note the key boldmessage is the same as {{ boldmessage }} in the template! 
context_dict = {'boldmessage': "I am bold font from the context"} 

# Return a rendered response to send to the client. 
# We make use of the shortcut function to make our lives easier. 
# Note that the first parameter is the template we wish to use. 

return render(request, 'rango/index.html', context_dict) 

對於評論的抱歉,經過幾次嘗試,我複製了網站的代碼,但仍然沒有成功。

我的文件結構如下:

  • 工作區

    • tango_with_django_project
      • manage.py
      • tango_with_django_project
        • 設置。PY
      • 模板
        • 蘭戈
          • 的index.html

要添加,這裏是顯示在127.0.0.1:8000/rango:

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: 
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 
/site-packages/django/contrib/admin/templates/rango/index.html (File does not exist) 
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 
/site-packages/django/contrib/auth/templates/rango/index.html (File does not exist) 

回答

7

TEMPLATE_DIRSdeprecated in django 1.8。您應該使用TEMPLATES設置。有機會,你已經在settings.pyTEMPLATES變量,以便改變DIRS關鍵是這樣的:

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

感謝,這爲我工作! – TomG466

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

添加此下方BASE_DIR:

PROJECT_PATH = os.path.join(BASE_DIR, os.pardir) 

下一頁以下應該看起來像這樣:

STATIC_PATH = os.path.join(PROJECT_PATH, 'tango_with_django_project/static') 

STATIC_URL = '/static/' 

STATICFILES_DIRS =(

    STATIC_PATH, 
) 

TEMPLATE_PATH = os.path.join(PROJECT_PATH, 'tango_with_django_project/templates') 

TEMPLATE_DIRS = (
    TEMPLATE_PATH, 
) 

MEDIA_URL = '/media/' 
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'tango_with_django_project/media') 
0

您的目錄結構錯誤。根據你的目錄結構此代碼返回查了一下這個代碼做什麼...

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

...

/root_path/tango_with_django_project/tango_with_django_project

因此,根據你的結構,模板文件夾不在文件夾tango_with_django_project內,這是它認爲它應該是。

如果需要,您可以直接指定模板的直接路徑。

TEMPLATE_PATH = '/root_project/tango_with_django_project/templates/' 

在附註中,帶Django的Tango是系列教程中最糟糕的名字,可能永遠都是。