gpt4 book ai didi

python - 模板不存在 at/books/index.html

转载 作者:行者123 更新时间:2023-12-01 03:02:47 25 4
gpt4 key购买 nike

我正在尝试使用 Django 版本 1.10 创建自己的项目,但是运行服务器时出现这样的异常:TemplateDoesNotExist at/books/index.html

这是我的文件夹结构:

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

设置.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

错误消息:

TemplateDoesNotExist at /books/ books/index.html Request Method: GET Request URL: http://localhost:8000/books/ Django Version: 1.10 Exception Type: TemplateDoesNotExist Exception Value:
books/index.html Exception Location: C:\Program Files\Python36\lib\site-packages\django\template\loader.py in select_template, line 53 Python Executable: C:\Program Files\Python36\python.exe Python Version: 3.6.1

谁能帮我找出问题所在吗?提前致谢。

最佳答案

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

关于python - 模板不存在 at/books/index.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43663644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com