在/ rango /的TemplateDoesNotExist

时间:2015-05-01 13:20:49

标签: django templates

我已尽力采购并解决此问题,但我似乎无法找到答案。

目前正在浏览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
        • settings.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)

3 个答案:

答案 0 :(得分:7)

TEMPLATE_DIRSdeprecated in django 1.8。您应该使用TEMPLATES设置。您可能已在TEMPLATES中拥有settings.py变量,因此请更改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',
            ],
        },
    },
]

答案 1 :(得分: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')

答案 2 :(得分: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/'

另一方面,Tango与Django是教程系列中最糟糕的名字,可能永远。

相关问题