隐藏在 Wagtail 中创建的新页面

Hiding new pages from being created in Wagtail

我已经尝试为此搜索官方文档和相关留言板,但是我一直无法找到任何相关内容。

我网站中的某些页面模板只能使用一次 - 例如首页。

在 Wagtail 中有没有办法隐藏或禁止用户在创建新页面时选择 template/Page 模型?

您可以通过编辑 YourPageModel.parent_page_types 来限制可以创建页面类型的位置,请参阅 http://docs.wagtail.io/en/v1.10.1/reference/pages/model_reference.html#wagtail.wagtailcore.models.Page.parent_page_types。类似地,还有一个 subpage_types 设置,因此例如您可以强制 NewsIndexPage 只能创建为 HomePage 的直接子级,并且只能包含 NewsItemPage 个实例。将此与用户权限相结合对于主页或页面类型在树中足够高的位置应该足够了。

如果您绝对必须强制只存在一种特定页面类型,则可以将 class 方法 clean_parent_page_models 重写为 return [] 如果此 class 的实例已经存在。然而,这种方法是一种 hack,可能会被 Wagtail 的未来版本所破坏。


2021 年 1 月更新:

上面的 hack 并不完全 broken,但它肯定不理想,并且自版本 2.4 Wagtail 具有 max_count Page 属性(和 max_count_per_parent 在 2.5).

文档:https://docs.wagtail.io/en/stable/reference/pages/model_reference.html#wagtail.core.models.Page.max_count

@JaredOzzy 的回答是正确的,并且对这些方法有更多的细节。

任何来这里寻找类似东西的人,Wagtail 现在添加了 2 个漂亮的属性,我们可以使用它们来限制页面的创建次数超过特定次数。

http://docs.wagtail.io/en/v2.5.1/reference/pages/model_reference.html#wagtail.core.models.Page.max_count

max_count

Controls the maximum number of pages of this type that can be created through the Wagtail administration interface. This is useful when needing “allow at most 3 of these pages to exist”, or for singleton pages.

max_count_per_parent.

Controls the maximum number of pages of this type that can be created under any one parent page.