django - Wagtail 模型的自定义 URL

标签 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/ 该页面有各种方法,例如 url、url_path 我应该覆盖哪一个以及实现某些目标的最佳实践是什么像这样的鹡鸰?

最佳答案

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

将该模块添加到您已安装的应用程序中:

INSTALLED_APPS = [
   ...

   "wagtail.contrib.routable_page",
]

继承wagtail.contrib.routable_page.models.RoutablePageMixinwagtail.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/

相关文章:

django - 在Django中的save()中执行delete()

wagtail - 通过 Meta 订购自定义模型实例?

python - Firebase 身份验证和 Django/Djangae

python - pymssql INSERT不会自动递增ID

django - 如何正确重命名 Wagtail 页面模型

python - 如何用鹡鸰称呼孙子

wagtail - 如何将 RichTextField 迁移到 StreamField?

python - 我可以使用 Django Channels 实时显示 MQTT 消息吗?

django - 在 Node.js 应用程序中使用 BCryptSHA256PasswordHasher 验证在 Django 中哈希的密码

javascript - Django:自动更新下拉菜单导致 "form.is_valid()"返回 false