python - Django 模板不存在@

标签 python django templates

我使用 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'

供您引用, Django official documentation

关于python - Django 模板不存在@,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63985013/

相关文章:

python - 查找列中字符串的精确正则表达式匹配

python - 使用 Django 下载 xlsx 格式的 SQL 数据

python - CherryPy 以 Cheetah 作为插件 + 工具 - 空白页

java - 为什么我无法在 Play 2 中将对象渲染到我的 View 中?

templates - Play 2.0 模板中 object.member 的模式匹配

python - 使用Python subprocess模块​​时如何传递变量

python - re.split ("", string) 和 re.split ("\s+", string) 之间的区别?

python - 这是我的函数或 Python 中的 docx.Document 的缺陷吗

django - 将 SSL 添加到 Django 应用程序、Ubuntu 16+、DigitalOcean

c++ - 我可以统一初始化模板类的静态常量成员吗?