我使用 python 3.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 并且它不在我的应用程序级别 urls.py 或 view.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'
设置.py:
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True,
最佳答案
如果您使用任何 CBV(基于类的 View ),默认情况下 django 将查找具有某些特定模式的模板。在您的情况下,由于您使用的是 ListView ,它将查找 YOURMODELNAME_list.html (小写的 YOURMODELNAME),如果您要扩展详细信息 View ,它将查找 YOURMODELNAME_detail.html 。如果您想在 CBV 中覆盖此行为试试这个,
class YourCBV(ListView):
template_name = 'your_new_template.html'
关于python - Django 模板不存在@,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63985013/