如何修复 Hugo CMS 中的错误“found no layout file for "HTML" for "page"”?

How to fix the error `found no layout file for "HTML" for "page"` in Hugo CMS?

我最近将我的 WordPress 页面中的内容导入了 Hugo。当我 运行 hugo serve 时,我收到以下错误消息:

WARN 2020/02/17 20:51:06 found no layout file for "HTML" for "page": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.

有问题的页面是这样开始的:

---
linktitle: "The Title of the Post"
title: "The Title of the Post"
author: "Franz Drollig"
type: post
date: 2020-01-09T14:41:55+00:00
url: /the-title-of-post/
categories:
  - Uncategorized
weight: 10

---


This is the content of the post. This is the second sentence of the post. 

位于mysite/content/posts/YYYY-MM-DD-title.md

layouts 目录是空的。但是,themes 目录包含 book 主题。 config.toml.

中也配置了这个主题

另一个 post,如下所示,正确呈现。

---
author: "Michael Henderson"
date: 2014-09-28
linktitle: Creating a New Theme
menu:
  main:
    parent: tutorials
next: /tutorials/github-pages-blog
prev: /tutorials/automated-deployments
title: Creating a New Theme
weight: 10
---


## Introduction

为什么我的 post 没有正确呈现?我该如何解决?

我没有使用 Hugo 的经验,但如果它能帮助您指明正确的方向,我想我会发表评论。

这两份文件的前言似乎大不相同。我认为 type: post 可能是罪魁祸首。

如果完全删除 post 前面的内容会怎样?

这些链接也可能有用:
Front Matter | Hugo
Hugo's Lookup Order | Hugo

您必须创建所需的布局;默认页面布局命名为single.html,部分布局命名为list.html。

即使您不会将它们用作真正的模板,您也必须创建它以避免出现 WARN。

因此,在 layout/_default 中创建这些文件:single.html list.html。另外,如果你觉得自己适用,可以在每个文件中使用 hugo 模板写一些评论,如下所示:

{{ "<!-- Layout with no content to avoid WARN message about missing page layout -->" | safeHTML }}

修复:

好的,这就是可能发生的情况:

您有一个作为 git 子模块添加的主题,并且您最近重新克隆了您的项目。你猜怎么着?您的子模块也需要重新下载。

您可以使用:

  • git 子模块初始化
  • git 子模块更新

然后您的项目将加载而不会出现错误。

如果你使用hugo academic主题,请试试这个:

$ hugo mod clean
$ hugo server

$ hugo mod clean
$ hugo mod get -u ./...
$ hugo server

参考: Error: File “not found” or “failed to extract” | Troubleshooting

就我而言,我错过了这一步:

echo theme = \"hugo-future-imperfect-slim\" >> config.toml

这会将您的主题添加到配置中。之后,它使用了主题中正确的 Kinds / Layout 元素。快速入门中的更多信息:https://gohugo.io/getting-started/quick-start/