【问题标题】:Getting This Error : TemplateDoesNotExist at post/1/出现此错误:TemplateDoesNotExist at post/1/
【发布时间】:2021-04-03 23:06:31
【问题描述】:

当我尝试访问显示此错误的页面时,任何人都可以帮助我解决此错误

异常类型:TemplateDoesNotExist

异常值:firstblog/post_detail.html

异常位置:C:\Users\ADMIN\Envs\test\lib\site-packages\django\template\loader.py,第 47 行,在 select_template 中

views.py

class PostDetailView(DetailView):
    model = Post

url.py

urlpatterns = [
    path('',PostListView.as_view(), name='blog-home'),
    path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
    path('about/',views.about, name='blog-about'),
]

【问题讨论】:

  • 问题解决了吗?请将任何正确答案标记为已接受以关闭它。
  • 我检查过,似乎一旦您接受并不能解决您的问题。请不要接受不正确的答案。如果您有自己的解决方案,您可以发布并接受,否则接受我发布的解决方案。 @bhavikjain

标签: python django django-templates


【解决方案1】:

您需要在基于类的通用视图中添加template_name 属性:

class PostDetailView(DetailView):
    model = Post
    template_name = 'firstblog/post_detail.html' 

【讨论】:

  • @BhavikJain 如果您的问题得到解决,请接受答案。
  • template_name 为default .... 错误提示的问题是post_detail.html 文件丢失。
【解决方案2】:

在基于类的视图中,如果您没有定义 template_name,Django 会查找标准模式 /_detail.html,在您的情况下为“firstblog/post_detail.html” ...这就是错误消息的位置来自。您需要创建 /firstblock/templates/firstblock/post_detail.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 2016-08-27
    • 2020-06-20
    • 2018-09-29
    • 2015-03-01
    • 2019-06-01
    • 2021-02-08
    相关资源
    最近更新 更多