我有一个像这样的模型:

from wagtail.wagtailcore.models import Page

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


现在,默认情况下,如果我的子弹是hi-there,则可以在site_url:/hi-there/上访问该博客文章,但我希望通过site:url/2014/02/05/hi-there/访问该页面。页面具有多种方法,例如url, url_path,我应该重写其中的一种,最好的方法是什么练习在w中实现这样的目标?

最佳答案

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
        """
        ...

关于django - Wagtail模型的自定义URL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25240599/

10-12 18:13