简体   繁体   中英

Paginator in django

Hi i looking at https://docs.djangoproject.com/en/2.1/topics/pagination/ documentation about django paginator

in the code it use

from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator

    from django.shortcuts import render

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

        page = request.GET.get('page')
        contacts = paginator.get_page(page)
        return render(request, 'list.html', {'contacts': contacts})

And in template it use

<div class="pagination">
    <span class="step-links">
        {% if contacts.has_previous %}
            <a href="?page=1">&laquo; first</a>
            <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>
            <a href="?page={{ contacts.paginator.num_pages }}">last &raquo;</a>
        {% endif %}
    </span>
</div>

I understand all the codes except in first time render what will be value of page

In the line

page = request.GET.get('page')

i know django run

?page=Value

and pass value to page argumant but what was the value of page in first render

在这种情况下, page将为None ,然后get_page将返回第一页。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM