RoutablePageMixin是当前 (v2.0+) 实现此目的的方法。
将模块添加到已安装的应用程序中:
INSTALLED_APPS = [
...
"wagtail.contrib.routable_page",
]
继承自wagtail.contrib.routable_page.models.RoutablePageMixin
and wagtail.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
"""
...