Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django pagination - example from documentation. How to display with all sites number?

Tags:

django

This is example from documentation Django:

def listing(request):
    contact_list = Contacts.objects.all()
    paginator = Paginator(contact_list, 25) # Show 25 contacts per page

    page = request.GET.get('page')
    try:
        contacts = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        contacts = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        contacts = paginator.page(paginator.num_pages)

    return render_to_response('list.html', {"contacts": contacts})

template:

{% for contact in contacts %}
    {# Each "contact" is a Contact model object. #}
    {{ contact.full_name|upper }}<br />
    ...
{% endfor %}

<div class="pagination">
    <span class="step-links">
        {% if contacts.has_previous %}
            <a href="?page={{ contacts.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}.
        </span>

        {% if contacts.has_next %}
            <a href="?page={{ contacts.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>

This display for example:

Page 2 of 3. next

How to display it in this way:

previous  1 <b>2</b> 3 Next

Current page with html <b> mark.

?

like image 831
user2426362 Avatar asked Jul 05 '13 14:07

user2426362


People also ask

How do I get the current page in pagination Django?

You can get the current objects on a page using {% autopaginate object_list %} , which replaces object_list with the current objects for any given page. You can iterate through it and if you want the first, you should be able to treat it like a list and do object_list[0] .

What is Paginator in Django?

Django provides a few classes that help you manage paginated data – that is, data that's split across several pages, with “Previous/Next” links. These classes live in django/core/paginator.py. For examples, see the Pagination topic guide.


1 Answers

You can try this:

{% for num in contacts.paginator.page_range %}
  {% ifequal num contacts.number %}
    <span class="current"><b>{{ num }}</b></span>
  {% else %}
    <a href="?page={{ num }}"> {{ num }}</a>
  {% endifequal %} 
{% endfor %}
like image 111
arturex Avatar answered Oct 10 '22 09:10

arturex