speedplane
speedplane

Reputation: 16121

Django Endless Pagination - Showing Current Number of Results (not pages)

I am using django's endless pagination for search results and I want to create a message at the top simply stating, for example:

showing 20 results of 124

Or even better:

showing results 20 to 40 of 124

I do not see any way to get the current number of results in the endless pagination documentation. How would you do this?

Upvotes: 1

Views: 857

Answers (4)

user3608721
user3608721

Reputation: 31

People tend to overcomplicate this. I found thee current_start_index and current_end_index functions fairly well documented in the source ( but not in the actual documentation ).

Here is how you would use them to achieve that.

{{ pages.current_start_index }} through {{ pages.current_end_index }} of {{ pages.total_count }} results

Hope this helps someone :D

Upvotes: 1

Aamir Rind
Aamir Rind

Reputation: 39659

I have wrote my own custom tag for that matter by using the existing tag:

from endless_pagination import (
    settings,
    utils,
)


@register.inclusion_tag("endless_pagination/show_more.html", takes_context=True)
def show_more_with_counts(context, per_page_count, total_count, first_page_count=0,
    verb='enteries', loading=settings.LOADING, show_total_in_end=True):
    # This template tag could raise a PaginationError: you have to call
    # *paginate* or *lazy_paginate* before including the showmore template.
    data = utils.get_data_from_context(context)
    page = data['page']
    # show the template only if there is a next page
    if page.has_next():
        request = context['request']
        page_number = page.next_page_number()
        # Generate the querystring.
        querystring_key = data['querystring_key']
        querystring = utils.get_querystring_for_page(
            request, page_number, querystring_key,
            default_number=data['default_number'])
        curr_page_num = int(request.GET.get(querystring_key, 1))
        if curr_page_num == 1:
            if first_page_count:
                start = first_page_count + 1
            else:
                start = per_page_count + 1
        else:
            if first_page_count:
                start = (curr_page_num * per_page_count) - first_page_count
            else:
                start = (curr_page_num * per_page_count) + 1
        end = (per_page_count + start) - 1
        if end > total_count:
            end = total_count
        label = 'Load %(start)s to %(end)s of %(total)s %(verb)s' % {
            'start': start, 'end': end, 'total': total_count, 'verb': verb}
        return {
            'label': label,
            'loading': loading,
            'path': data['override_path'] or request.path,
            'querystring': querystring,
            'querystring_key': querystring_key,
            'request': request,
            'show_total_in_end': show_total_in_end,
        }
    else:
        if total_count > 0:
            return {
                'label': 'Showing %(start)s of %(end)s %(verb)s' % \
                    {'start': total_count, 'end': total_count, 'verb': verb},
                'show_total_in_end': show_total_in_end,
            }
        else:
            return {}

Also I have following show_more.html template:

{% load i18n %}
{% if querystring %}
    <div class="endless_container">
        <a class="endless_more" href="{{ path }}{{ querystring }}"
            rel="{{ querystring_key }}" style="font-size: 11px; color: #c13923;">{{ label }}</a>
        <div class="endless_loading" style="display: none;">{{ loading|safe }}</div>
    </div>
{% elif show_total_in_end %}
    <a href="#" style="text-decoration: none; color: #999; cursor:default; font-size: 11px;" onclick='return false;'>{{ label }}</a>
{% endif %}

How to use:

{% show_more_with_counts 10 qs_count verb='users' %}
# it will say `Load 1 to 10 of 100 users` for first page
# it will say `Load 11 to 20 of 100 users` for 2nd page, and so on

You have to pass per_page_count, total number of objects in a queryset or list total_count, and the verb to use.

Upvotes: 0

Pablo Abdelhay
Pablo Abdelhay

Reputation: 1008

Solution:

After using the paginate template tag, call {% get_pages %} and set the total results as a variable like this:

{% paginate items %}
{% get_pages %}
{% with total=pages.total_count %}

<div>Total results: {{ total }}</div>

{% for item in items %}
 ...
{% endfor %}

{% endwith %}

Upvotes: 1

pynovice
pynovice

Reputation: 7752

I would do as follows:

Count the total number of entries from database and pass to the template that would be 124. and then use paginate limit to limit the number of total entries per page. Count that and show however you want.

Upvotes: 0

Related Questions