TemplateDoesNotExist:错误的模板路径

时间:2015-08-27 14:11:41

标签: python django python-2.7 python-3.x django-templates

我在Ubuntu上开发基本的Web应用程序。我的项目目录结构如下所示:

tango
   -rango
      -migrations
   -tango
   -templates
      -rango
         -index.html

/tango/urls.py

from django.conf.urls import include, url, patterns
from django.contrib import admin

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'tango_with_django_project_17.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^rango/', include('rango.urls')), # ADD THIS NEW TUPLE!
)

兰戈/ urls.py

from django.conf.urls import patterns, url
from rango import views

urlpatterns = patterns('',
        url(r'^$', views.index, name='index'))

settings.py

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

TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')

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):
    context_dict = {'boldmessage': "I am bold font from the context"}

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

的index.html

<!DOCTYPE html>
<html>

    <head>
        <title>Rango</title>
    </head>

    <body>
        <h1>Rango says...</h1>
        hello world! <strong>{{ boldmessage }}</strong><br />
        <a href="/rango/about/">About</a><br />
    </body>

</html>

但是,当我尝试访问http://localhost:8000/rango/时,这是我遇到的错误:

Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/manas/D_Drive/Django/tango/rango/views.py" in index
  14.     return render(request, 'rango/index.html', context_dict)
File "/usr/local/lib/python2.7/dist-packages/django/shortcuts.py" in render
  67.             template_name, context, request=request, using=using)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in render_to_string
  98.             template = get_template(template_name, using=using)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in get_template
  46.     raise TemplateDoesNotExist(template_name)

Exception Type: TemplateDoesNotExist at /rango/
Exception Value: rango/index.html

index.html存储在以下路径中:/home/manas/D_Drive/Django/tango/templates/rango/index.html

我正在使用Django 1.8和Python 2.7并在Ubunty 14.04上运行该应用程序。

这里看起来有什么问题?

编辑:

模板设置:

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',
            ],
        },
    },
]

2 个答案:

答案 0 :(得分:3)

'DIRS'配置中有TEMPLATES个空列表,因此无法找到您的模板文件夹。 只需将其替换为:'DIRS': [os.path.join(BASE_DIR, 'templates')]'DIRS': [TEMPLATE_PATH],它就可以了,

答案 1 :(得分:0)

请务必将您的应用添加到settings.py

INSTALLED_APPS = [
    'myapp',
    ...,
    ...,
]