Python Wagtail在索引页中呈现任意路径

Python Wagtail在索引页中呈现任意路径,python,django,wagtail,Python,Django,Wagtail,我需要使一些页面能够编写不依赖于站点结构的任意URL class IndexPage(RoutablePageMixin, Page): ... @route(r'^(?P<path>.*)/$') def render_page_with_special_path(self, request, path, *args, **kwargs): pages = Page.objects.not_exact_type(IndexPage).spec

我需要使一些页面能够编写不依赖于站点结构的任意URL

class IndexPage(RoutablePageMixin, Page):
    ...
    @route(r'^(?P<path>.*)/$')
    def render_page_with_special_path(self, request, path, *args, **kwargs):
        pages = Page.objects.not_exact_type(IndexPage).specific()
        for page in pages:
            if hasattr(page, 'full_path'):
                if page.full_path == path:
                    return page.serve(request)
        # some logic
例如,我有一个结构:

/
/blog
/blog/blogpost1
/blog/blogpost2
但是,例如,我需要将url从
/blog/blbogpost2
更改为
/some/blogpost/url1

为此,我决定给这个机会来处理网站主页的任何URL

class IndexPage(RoutablePageMixin, Page):
    ...
    @route(r'^(?P<path>.*)/$')
    def render_page_with_special_path(self, request, path, *args, **kwargs):
        pages = Page.objects.not_exact_type(IndexPage).specific()
        for page in pages:
            if hasattr(page, 'full_path'):
                if page.full_path == path:
                    return page.serve(request)
        # some logic
class索引页面(RoutablePageMixin,第页):
...
@路线(r’^(?P.*)/$)
def render_page_,带有特殊路径(self、request、path、*args、**kwargs):
pages=Page.objects.not_-exact_-type(IndexPage).specific()
对于页面中的页面:
如果hasattr(第页“完整路径”):
如果page.full_path==路径:
返回页面。送达(请求)
#一些逻辑

但是现在,如果没有找到这个
路径
,但是我需要将这个请求返回给标准处理程序。我该怎么做?

这在
RoutablePageMixin
中是不可能的;Wagtail将URL路由和页面服务视为两个不同的步骤,一旦确定了负责服务页面的功能(对于
RoutablePageMixin
,这是通过检查
@route
中给出的URL路由来完成的),就无法返回URL路由步骤

但是,它可以通过,这是低级机制来完成。您的版本将如下所示:

from wagtail.core.url_routing import RouteResult

class IndexPage(Page):
    def route(self, request, path_components):
        # reconstruct the original URL path from the list of path components
        path = '/'
        if path_components:
            path += '/'.join(path_components) + '/'

        pages = Page.objects.not_exact_type(IndexPage).specific()
        for page in pages:
            if hasattr(page, 'full_path'):
                if page.full_path == path:
                    return RouteResult(page)

        # no match found, so revert to the default routing mechanism
        return super().route(request, path_components)

谢谢你的帮助!