Question

middleware.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

I have a problem: when i want to turn zero into URL as GET parameter (?perpage=0), it shows me ZeroDivisionError float division by zero. I need to take ALL objects on page without pagination when perpage=0. How can I do this? What is must be in view.py?

Was it helpful?

Solution

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''

when my view gets perpage=0, it returns pure object list (value) without Pagination to template

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top