如何在 Wagtail 中获得 "flatter" URL?
How can I get a "flatter" URL in Wagtail?
我有一个直接位于根页面内的 DestinationIndexPage
模型,其中有多个 DestinationPage
实例。
可以使用这样的 URL 访问它们:
- /destinations/london
- /destinations/birmingham
- /destinations/manchester
有没有办法将目标页面保留在 DestinationIndexPage
中,但它们是从以下 URL 提供的?
- /伦敦
- /伯明翰
- /曼彻斯特
这是为了让 Wagtail 管理井井有条,同时也防止深度嵌套的 URL。
因为我只会隐藏一个 child page
,所以我为一个页面创建了几个混入 "conceal"。采用以下层次结构:
HomePage --> Destination Index --> Destinations
HomePage
会有这个混音:
class ConcealedChildMixin(Page):
"""
A mixin to provide functionality for a child page to conceal it's
own URL, e.g. `/birmingham` instead of `/destination/birmingham`.
"""
concealed_child = models.ForeignKey(
Page,
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name='concealed_parent',
help_text="Allow one child the ability to conceal it's own URL.",
)
content_panels = Page.content_panels + [
FieldPanel('concealed_child')
]
class Meta:
abstract = True
def route(self, request, path_components):
try:
return super().route(request, path_components)
except Http404:
if path_components:
subpage = self.specific.concealed_child
if subpage and subpage.live:
return subpage.specific.route(
request, path_components
)
raise Http404
并且 Destinations
会有这个混入:
class ConcealedURLMixin:
"""
A mixin to provide functionality for a page to generate the correct
URLs if it's parent is concealed.
"""
def set_url_path(self, parent):
"""
Overridden to remove the concealed page from the URL.
"""
if parent.concealed_parent.exists():
self.url_path = (
'/'.join(
[parent.url_path[: len(parent.slug) - 2], self.slug]
)
+ '/'
)
else:
self.url_path = super().set_url_path(parent)
return self.url_path
我有一个直接位于根页面内的 DestinationIndexPage
模型,其中有多个 DestinationPage
实例。
可以使用这样的 URL 访问它们:
- /destinations/london
- /destinations/birmingham
- /destinations/manchester
有没有办法将目标页面保留在 DestinationIndexPage
中,但它们是从以下 URL 提供的?
- /伦敦
- /伯明翰
- /曼彻斯特
这是为了让 Wagtail 管理井井有条,同时也防止深度嵌套的 URL。
因为我只会隐藏一个 child page
,所以我为一个页面创建了几个混入 "conceal"。采用以下层次结构:
HomePage --> Destination Index --> Destinations
HomePage
会有这个混音:
class ConcealedChildMixin(Page):
"""
A mixin to provide functionality for a child page to conceal it's
own URL, e.g. `/birmingham` instead of `/destination/birmingham`.
"""
concealed_child = models.ForeignKey(
Page,
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name='concealed_parent',
help_text="Allow one child the ability to conceal it's own URL.",
)
content_panels = Page.content_panels + [
FieldPanel('concealed_child')
]
class Meta:
abstract = True
def route(self, request, path_components):
try:
return super().route(request, path_components)
except Http404:
if path_components:
subpage = self.specific.concealed_child
if subpage and subpage.live:
return subpage.specific.route(
request, path_components
)
raise Http404
并且 Destinations
会有这个混入:
class ConcealedURLMixin:
"""
A mixin to provide functionality for a page to generate the correct
URLs if it's parent is concealed.
"""
def set_url_path(self, parent):
"""
Overridden to remove the concealed page from the URL.
"""
if parent.concealed_parent.exists():
self.url_path = (
'/'.join(
[parent.url_path[: len(parent.slug) - 2], self.slug]
)
+ '/'
)
else:
self.url_path = super().set_url_path(parent)
return self.url_path