TemplateDoesNotExist - 创建一个简单的视图

时间:2016-03-09 02:54:22

标签: python django

我有以下树形结构:

project/
  __init__.py
  app/
    __init__.py
    templates/
       home.html
       base.html
    settings.py
    urls.py
    views.py
    wsgi.py

views.py

from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.template.loader import get_template

def home(request):
   context = RequestContext(request,
                           {'user': request.user})
   return render_to_response('templates/home.html',
                             context_instance=context)

urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', 'app.views.home', name='home'),

]

settings.py的相关部分

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app'
]

...

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

问题:当我跑步时:

  1. ./ manage runserver
  2. 导航至127.0.0.1:8000
  3. 它引发django.template.exceptions.TemplateDoesNotExist: templates/home.html
  4. 我尝试了不同的路径变体以及将目录硬编码到我的设置文件中的TEMPLATES字典中。我错过了什么?

1 个答案:

答案 0 :(得分:0)

此设置很好。问题是我在home.html中扩展了base.html

赞:{% extends 'app/base.html' %}需要{% extends 'base.html' %}