1

中间件.py

def get_perpage(self):
    try:
        self.session['perpage'] = int(self.REQUEST['perpage'])
        return self.session['perpage']
    except (KeyError, ValueError, TypeError):
        pass

    try:
        return int(self.session['perpage'])
    except (KeyError, ValueError, TypeError):
        return DEFAULT_PAGINATION

我有一个问题:当我想将零作为 GET 参数 (?perpage=0) 转换为 URL 时,它显示 ZeroDivisionError 浮点除以零。当 perpage=0 时,我需要在页面上获取所有对象而不进行分页。我怎样才能做到这一点?view.py 中必须包含什么?

4

1 回答 1

0
def render(self, context):
    key = self.queryset_var.var
    value = self.queryset_var.resolve(context)
    if (self.paginate_by == None):
        paginate_by = int(context['request'].perpage)
    else:
        paginate_by = self.paginate_by.resolve(context)
    if (paginate_by == 0):          #HERE
        context['page_obj'] = value # IS 
        return u''                  #SOLUTION
    print (paginate_by)
    paginator = Paginator(value, paginate_by, self.orphans)
    try:
        page_obj = paginator.page(context['request'].page)
    except InvalidPage:
        if INVALID_PAGE_RAISES_404:
            raise Http404('Invalid page requested.  If DEBUG were set to ' +
                'False, an HTTP 404 page would have been shown instead.')
        context[key] = []
        context['invalid_page'] = True
        return u''
    if self.context_var is not None:
        context[self.context_var] = page_obj.object_list
    else:
        context[key] = page_obj.object_list 
    context['paginator'] = paginator
    context['page_obj'] = page_obj
    return u''

当我的视图获取 perpage=0 时,它返回纯对象列表(值),没有分页到模板

于 2012-08-30T18:16:51.483 回答