我有一个在一般 Django 应用程序中使用的日历,但我也想在该网站的 Wagtail 应用程序(显示一般文本内容/博客)中显示它。
我已经可以在 Wagtail 页面上显示日历,并手动输入日期参数了。其模型如下所示:
class SchedulePage(PostPageExtras, RoutablePageMixin, Page):
content_panels = Page.content_panels
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
context['schedule'] = self.get_calendar()
return context
def get_calendar(self):
the_year = 2024
the_month = 7
cal = ScheduleCalendar().formatmonth(the_year, the_month, withyear=True)
return cal
我尝试用
RoutablePageMixin
解决这个问题,但没有取得任何进展。我可以生成从 the_year
派生的 the_month
和 .now()
值,因此日历始终显示当前月份。但我希望用户能够导航到下个月/上个月。
在 Django 应用程序中完成此操作的方式是有两个 url 指向同一个视图,并且视图根据需要解释参数:
path('/schedule/', views.ScheduleCalendarView.as_view(), name='control-schedule-calendar'),
re_path(r'^/schedule/(?P<year>\d+)/(?P<month>\d+)/$', views.ScheduleCalendarView.as_view(), name='control-schedule-monthview'),
使用这两个 URL,您将获得相同的页面显示(2024 年 7 月):
https://example.com/schedule/
https://examle.com/schedule/2024/07/
因为,在第一个示例中,如果 URL 中不存在
the_year
/the_month
参数,视图将创建这些参数。
还有第三个 URL,具有不同的视图,显示特定日期的详细信息:
re_path(r'^/schedule/(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/$', views.ControlScheduleDayView.as_view(), name='control-schedule-dayview'),
这在常规 Django 中相当简单。
我想我想遵循相同的模式,或者三个 URL,并且可能不需要日视图的第二个视图,如果
day
参数位于 URL 中,就会有某种切换。
如何在 Wagtail 模型中做到这一点?或者,如何在一般 Django 环境中加载 Wagtail/博客模板并显示 Wagtail/博客导航?
提前致谢。
我们这样做——事实上,这是我们对同一模式的第二次迭代。将代码提取到要点中更容易:https://gist.github.com/cnk/603c48f6029e033d38e006db6988843a