自定义页面模型的 Wagtail url 前缀
Wagtail url prefix for a custom Page model
这个问题可能很简单,但我找不到简单的解决方案。
我有代表 Post:
的自定义页面模型
class PostPage(Page):
我想让这个模型的所有实例(所有 Posts)只能通过 url 前缀访问
/posts/
示例:
用户创建新的 Post,分配的 slug 将是
awesome-first-post
应该发生的是,
/awesome-first-post/
将导致 404,而
/posts/awesome-first-post/
将显示 post。
注意:我只希望这个前缀用于特定型号 Post页面。其他页面应直接从其 slug 提供。
在 Wagtail 中,页面 URL 由页面的父页面和祖先页面的 slug 列表组成,基于页面在树中的位置 - 开发人员不会直接指定它们。因此,要获得 URL /posts/awesome-first-post/
,请创建一个带有 slug posts
的页面(通常您会创建一个专用的 PostIndexPage
页面模型作为列表页面),并创建页面 awesome-first-post
作为该页面的子页面(通过单击资源管理器列表视图中“帖子”页面旁边的“+”图标)。
如果您想确保用户只创建 PostPage
作为 PostIndexPage
的子代,请使用 subpage_types
/ parent_page_types
setting,例如:
class PostPage(Page):
# ...
parent_page_types = ['PostIndexPage']
这个问题可能很简单,但我找不到简单的解决方案。
我有代表 Post:
的自定义页面模型class PostPage(Page):
我想让这个模型的所有实例(所有 Posts)只能通过 url 前缀访问
/posts/
示例:
用户创建新的 Post,分配的 slug 将是
awesome-first-post
应该发生的是,
/awesome-first-post/
将导致 404,而
/posts/awesome-first-post/
将显示 post。
注意:我只希望这个前缀用于特定型号 Post页面。其他页面应直接从其 slug 提供。
在 Wagtail 中,页面 URL 由页面的父页面和祖先页面的 slug 列表组成,基于页面在树中的位置 - 开发人员不会直接指定它们。因此,要获得 URL /posts/awesome-first-post/
,请创建一个带有 slug posts
的页面(通常您会创建一个专用的 PostIndexPage
页面模型作为列表页面),并创建页面 awesome-first-post
作为该页面的子页面(通过单击资源管理器列表视图中“帖子”页面旁边的“+”图标)。
如果您想确保用户只创建 PostPage
作为 PostIndexPage
的子代,请使用 subpage_types
/ parent_page_types
setting,例如:
class PostPage(Page):
# ...
parent_page_types = ['PostIndexPage']