我有一个这样的模型:
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/
该页面有各种方法,例如 url、url_path
我应该覆盖哪一个以及实现某些目标的最佳实践是什么像这样的鹡鸰?
最佳答案
RoutablePageMixin是当前(v2.0+)实现此目的的方法。
将该模块添加到您已安装的应用程序中:
INSTALLED_APPS = [
...
"wagtail.contrib.routable_page",
]
继承wagtail.contrib.routable_page.models.RoutablePageMixin
和wagtail.core.models.Page
,然后定义一些 View 方法并用装饰它们>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
"""
...
关于django - Wagtail 模型的自定义 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25240599/