Wagtail模型的自定义URL

时间:2014-08-11 10:00:18

标签: django wagtail

我有一个这样的模型:

from wagtail.wagtailcore.models import Page

class Blog(Page):
    created = models.DateTimeField(auto_now_add=True)
    ...
    ..

现在,我的默认设置,如果我的slug是hi-there,则可以在site_url:/hi-there/访问博客帖子,但我希望可以通过site:url/2014/02/05/hi-there/访问该页面。该页面有各种方法,例如{{1}我应该覆盖哪一个以及在wagtail中实现这样的事情的最佳实践是什么?

2 个答案:

答案 0 :(得分:2)

Wagtail v0.5中的新功能是一种直接解决此类问题的机制:

v1.3中的

Embedding URL configuration in PagesRoutablePage

(文档甚至有博客示例!)

答案 1 :(得分:2)

RoutablePageMixin是当前(v2.0 +)实现此目的的方法。

将模块添加到已安装的应用中:

INSTALLED_APPS = [
   ...

   "wagtail.contrib.routable_page",
]

继承wagtail.contrib.routable_page.models.RoutablePageMixinwagtail.core.models.Page,然后定义一些视图方法并使用wagtail.contrib.routable_page.models.route装饰器装饰它们:

from wagtail.core.models import Page
from wagtail.contrib.routable_page.models import RoutablePageMixin, route

class EventPage(RoutablePageMixin, Page):
    ...

    @route(r'^$') # will override the default Page serving mechanism
    def current_events(self, request):
        """
        View function for the current events page
        """
        ...

    @route(r'^past/$')
    def past_events(self, request):
        """
        View function for the past events page
        """
        ...

    # Multiple routes!
    @route(r'^year/(\d+)/$')
    @route(r'^year/current/$')
    def events_for_year(self, request, year=None):
        """
        View function for the events for year page
        """
        ...