TemplateDoesNotExist位于/​​books/index.html

时间:2017-04-27 16:48:23

标签: python django

我正在尝试使用Django版本1.10创建我自己的项目,但运行服务器我得到了这样的例外:/ books / index.html上的TemplateDoesNotExist

这是我的文件夹结构:

mysite/
  books/
    migrations
    template/
      books/
        index.html
    __init__.py
    admin.py
    apps.py
    models.py
    tests.py
    urls.py
    views.py
  mysite/
    __init__.py
    dbsqlite3
    settings.py
    urls.py
    wsgi.py
  tamplates/
    base_books.html
    base.html
  dbsqlite3
  manage.py

settings.py:

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

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],
        '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',
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'

mysite / books / urls.py:

from django.conf.urls import url, include
from books import views

urlpatterns = [
    url(r'^$', views.BooksModelView.as_view(), name='index'),

    url(r'^books/$', views.BookList.as_view(), name='books_list'),
    url(r'^author/$', views.AuthorList.as_view(), name='author_list'),
    url(r'^publisher/$', views.PublisherList.as_view(), name='publisher_list'),

    url(r'^books/(?P<pk>\d+)/$', views.BookDetail.as_view(), name='detail_list'),
    url(r'^author/(?P<pk>\d+)/$', views.AuthorDetail.as_view(), name='author_detail'),
    url(r'^publisher/(?P<pk>\d+)/$', views.PublisherDetail.as_view(), name='publisher_detail'),
]

mysite / mysite / urls.py:

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

urlpatterns = [
    url(r'^books/', include('books.urls', namespace="books")),
    url(r'^admin/', admin.site.urls),
]

views.py:

from django.views.generic.base import TemplateView
from django.views.generic import ListView
from django.views.generic import DetailView
from books.models import Book, Author, Publisher

# TemplateView
class BooksModelView(TemplateView):
    template_name = 'books/index.html'

    def get_context_data(self, **kwargs):
        context = super(BooksModelView, self).get_context_data(**kwargs)
        context['object_list'] = ['Book', 'Author', 'Publisher']
        return context

#--- ListView
class BookList(ListView):
    model = Book

class AuthorList(ListView):
    model = Author

class PublisherList(ListView):
    model = Publisher

#--- DetailView
class BookDetail(DetailView):
    model = Book

class AuthorDetail(DetailView):
    model = Author

class PublisherDetail(DetailView):
    model = Publisher

错误消息:

  

/ books / books / index.html上的TemplateDoesNotExist请求方法:GET   请求URL:http://localhost:8000/books/ Django版本:1.10   例外类型:TemplateDoesNotExist异常值:
  books / index.html异常位置:C:\ Program   Files \ Python36 \ lib \ site-packages \ django \ template \ loader.py in   select_template,第53行Python可执行文件:C:\ Program   Files \ Python36 \ python.exe Python版本:3.6.1

任何人都可以帮我识别问题吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

将文件夹从book/template重命名为book/templates