2015-05-01 208 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是系列教程中最糟糕的名字,可能永远都是。