问题描述
继续,,我如何按周分页?
In continuation of my struggle with WeekArchiveView
, how do I paginate it by week?
我想要的是:
- 知道是否有下一个/上一个一周可用;
- 如果有,提供模板中的链接。
我'd喜欢也可以跳过空周。
I'd like it to also skip empty weeks.
显示 get_next_day
/ get_prev_day
和 get_next_month
/ get_prev_month
可用,但几周没有。
The source shows get_next_day
/ get_prev_day
and get_next_month
/ get_prev_month
are available, but nothing for weeks.
推荐答案
这是非常有趣的。确定 MonthMixin
包括 get_next_month
/ get_prev_month
方法, code> DayMixin 包含 get_next_day
/ get_prev_day
方法。但是,YearMixin和WeekMixin在定义中都没有功能上的等价物。似乎有点像Django团队的一个监督。
That is definitely interesting. Sure enough MonthMixin
includes get_next_month
/get_prev_month
methods, and DayMixin
includes get_next_day
/get_prev_day
methods. However, both YearMixin and WeekMixin have no functional equivalent in their definitions. Seems like a bit of an oversight on the Django team's part.
我认为你最好的选择是将WeekArchiveView或BaseWeekArchiveView子类化(如果最终可能要更改响应格式,并且不想重新实现您的方法)并添加您自己的 get_next_week
/ get_prev_week
方法。然后让你的视图继承自你的子类。一个简单的修改 DayMixin
的方法应该是足够的。
I think your best bet is to subclass either WeekArchiveView or BaseWeekArchiveView (if you may eventually want to change up the response format and don't want to have to re-implement your methods) and add your own get_next_week
/get_prev_week
methods. Then have your view inherit from your subclass instead. A simple modification of DayMixin
s methods should be sufficient.
def get_next_week(self, date):
"""
Get the next valid week.
"""
next = date + datetime.timedelta(days=7)
return _get_next_prev_month(self, next, is_previous=False, use_first_day=False)
def get_previous_week(self, date):
"""
Get the previous valid week.
"""
prev = date - datetime.timedelta(days=7)
return _get_next_prev_month(self, prev, is_previous=True, use_first_day=False)
这篇关于如何分页WeekArchiveView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!