Example #1
0
    def get_paginator(self, queryset, limit):
        paginator = Paginator(queryset, per_page=limit)
        paginator.has_limit = bool(limit)

        # Cache count for paginator to prevent redundant calls between requests
        if settings.DATA_CACHE_ENABLED:
            key = cache_key('paginator', kwargs={
                'queryset': queryset
            })

            count = cache.get(key)

            if count is None:
                count = _count(queryset)
                cache.set(key, count)
        else:
            count = _count(queryset)

        paginator._count = count

        if not limit:
            # Prevent division by zero error in case count is zero
            paginator.per_page = max(count, 1)

        return paginator
Example #2
0
    def get_paginator(self, queryset, limit):
        paginator = Paginator(queryset, per_page=limit)
        paginator.has_limit = bool(limit)

        # Perform count an update paginator to prevent redundant call
        if not limit:
            count = len(queryset)
            paginator.per_page = count
            paginator._count = count

        return paginator
Example #3
0
    def get_paginator(self, queryset, limit):
        paginator = Paginator(queryset, per_page=limit)
        paginator.has_limit = bool(limit)

        # Perform count an update paginator to prevent redundant call
        if not limit:
            count = len(queryset)
            # Prevent division by zero error in case count is zero
            paginator.per_page = max(count, 1)
            paginator._count = count

        return paginator
Example #4
0
def bus_stations(request, page=None):
    # Определение номеров страниц
    prev_page_url = None
    current_page = 1
    if page:
        current_page = page
    if current_page > 1:
        prev_page_url = current_page - 1
    # Задаем пагинатор
    list_station_paginator = Paginator(list_station, len(list_station))
    list_station_paginator.per_page = 5

    return render(request,
                  'index.html',
                  context={
                      'bus_stations':
                      list_station_paginator.get_page(current_page),
                      'current_page': current_page,
                      'prev_page_url': current_page - 1,
                      'next_page_url': current_page + 1,
                  })
Example #5
0
    def get_paginator(self, queryset, limit):
        paginator = Paginator(queryset, per_page=limit)
        paginator.has_limit = bool(limit)

        # Cache count for paginator to prevent redundant calls between requests
        if settings.DATA_CACHE_ENABLED:
            key = cache_key('paginator', kwargs={'queryset': queryset})

            count = cache.get(key)

            if count is None:
                count = _count(queryset)
                cache.set(key, count)
        else:
            count = _count(queryset)

        paginator._count = count

        if not limit:
            # Prevent division by zero error in case count is zero
            paginator.per_page = max(count, 1)

        return paginator