我使用的是Python3.7.2和Django 2.1,每次尝试加载主url时都会出现以下错误
TemplateDoesNotExist at /
ghostwriters/post_list.html
Request Method: GET Request URL: http://localhost:8080/ Django Version: 2.1 Exception Type: TemplateDoesNotExist Exception Value:
ghostwriters/post_list.html
Exception Location: C:\Users\User.virtualenvs\ghostwriter-HT06mH6q\lib\site-packages\django\template\loader.py in select_template, line 47 Python Executable: C:\Users\User.virtualenvs\ghostwriter-HT06mH6q\Scripts\python.exe
没有任何意义,因为确实没有post_list.html,它不在我的应用程序级别url.py或my views.py中,那么为什么会发生这种情况
URL.py:
from django.urls import path from .views import PostListView
urlpatterns = [ path('', PostListView.as_view(), name='home'), ]
views.py:
from django.shortcuts import render from django.views.generic import ListView
from .models import Post
class PostListView(ListView): model = Post template = 'home.html'
settings.py:
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True,
如果您使用的是任何CBV(基于类的视图),默认情况下django将查找具有特定模式的模板。在您的情况下,由于您使用的是列表视图,它将查找YOURMODELNAME_List.html(YOURMODELNAME小写),如果您扩展的是详细信息视图,它将查找YOURMODELNAME_Detail.html。如果您要覆盖此行为,请在您的CBV中尝试此操作
供参考, Django official documentation
相关问题 更多 >
编程相关推荐