Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为WeekArchiveView分页?_Python_Django_Django 1.3_Django Class Based Views_Django Pagination - Fatal编程技术网

Python 如何为WeekArchiveView分页?

Python 如何为WeekArchiveView分页?,python,django,django-1.3,django-class-based-views,django-pagination,Python,Django,Django 1.3,Django Class Based Views,Django Pagination,作为with的延续,如何按周分页 我想要的是: 了解是否有下一周/上一周可用 如果有,请在模板中提供链接 我希望它也能跳过空周 显示get_next_day/get_prev_day和get_next_month/get_prev_month可用,但几周内没有任何内容。这确实很有趣。果然MonthMixin包括get\u next\u month/get\u prev\u month方法,而DayMixin包括get\u next\u day方法。然而,YearMixin和WeekMixin

作为with的延续,如何按周分页

我想要的是:

  • 了解是否有下一周/上一周可用
  • 如果有,请在模板中提供链接
我希望它也能跳过空周


显示
get_next_day
/
get_prev_day
get_next_month
/
get_prev_month
可用,但几周内没有任何内容。

这确实很有趣。果然
MonthMixin
包括
get\u next\u month
/
get\u prev\u month
方法,而
DayMixin
包括
get\u next\u day
方法。然而,YearMixin和WeekMixin在其定义中都没有功能等价物。看起来Django团队有点疏忽

我认为您最好的选择是将WeekArchiveView或BaseWeekArchiveView子类化(如果您最终想要更改响应格式,并且不想重新实现您的方法),然后添加您自己的
get\u next\u week
/
get\u prev\u week
方法。然后让视图从子类继承。对
DayMixin
s方法进行简单修改就足够了

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)
以此为基础,我创建了一个类,该类为模板提供
next\u week
previous\u week

class BetterWeekArchiveView(WeekArchiveView):

    def get_next_week(self, date):
        """
        Get the next valid week.
        """
        next = date + 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 - timedelta(days=7)
        return _get_next_prev_month(self, prev, is_previous=True, use_first_day=False)

    def get_dated_items(self):
        """
        Return (date_list, items, extra_context) for this request.
        Inject next_week and previous_week into extra_context.
        """
        result = super(BetterWeekArchiveView, self).get_dated_items()
        extra_context = result[2]
        date = extra_context['week']

        extra_context.update({
            'next_week': self.get_next_week(date),
            'previous_week': self.get_previous_week(date),
        })

        return result

这很好用。

这对我真的很有帮助!谢谢。你能帮我吗?我们就快到了,我只是在寻找一种干净的方法,在给定的一周之前没有物品的情况下,让
前一周
成为
。哎呀!这是我的错。我将
allow_empty
设置为
True
,这就是我得到这些空页面的原因。再次非常感谢!