Django 分頁器

分液器

在頁面顯示分頁數據,須要用到Django分頁器組件css

from django.core.paginator import Paginatorhtml

Paginator對象:    paginator = Paginator(user_list, 10)
per_page: 每頁顯示條目數量
count:    數據總個數
num_pages:總頁數
page_range:總頁數的索引範圍,如: (1,10),(1,200)
page:     page對象    
page對象:page=paginator.page(1)
has_next              是否有下一頁
next_page_number      下一頁頁碼
has_previous          是否有上一頁
previous_page_number  上一頁頁碼
object_list           分頁以後的數據列表
number                當前頁
paginator             paginator對象

 視圖層

def index(request):

    # li=[]
    # for i in range(100):
    #     li.append(models.Book(name='圖書%s'%i,price=10+i))
    #數據庫批量新建數據
    # models.Book.objects.bulk_create(li)
    # return HttpResponse('ok')

    #獲取book表內的全部數據
    books = models.Book.objects.all()
    #Paginator( object_list, per_page)  object_list是要傳入一個對象列表,per_page這一頁顯示多少條數據
    paginator = Paginator(books, 3)
    try:
        #取出前臺傳過來的頁碼
        current_page_num = int(request.GET.get('page'))
        #生成當前頁的對象
        current_page = paginator.page(current_page_num)
        #判斷總頁數是否是大於11
        if paginator.num_pages > 11:
           #判斷前臺傳過來的頁碼-5是否是小於1
            if current_page_num - 5 < 1:
                # 當前頁碼數-5小於1,總頁碼1,到總頁碼12之間
                pagerange = range(1, 12)
            # 判斷當前頁碼數+5大因而否總頁碼數
            elif current_page_num + 5 > paginator.num_pages:
                # 當前頁碼數+5大於總頁碼數,總頁碼-10,到總頁碼+1之間
                pagerange = range(paginator.num_pages - 10, paginator.num_pages + 1)
            else:
                #其餘狀況,左5,右6推算,也就是:range(current_page_num - 5, current_page_num + 6)
                pagerange = range(current_page_num - 5, current_page_num + 6)
        else:
            #小於11,有多少頁,就顯示多少頁
            pagerange = paginator.page_range
    except Exception as e:
        pagerange = range(1, 12)
        current_page_num = 1
        current_page = paginator.page(current_page_num)
    return render(request, 'index.html', locals())

'''
分頁器的使用:

    book_list=Book.objects.all()

    paginator = Paginator(book_list, 10)

    print("count:",paginator.count)           #數據總數
    print("num_pages",paginator.num_pages)    #總頁數
    print("page_range",paginator.page_range)  #頁碼的列表

    page1=paginator.page(1) #第1頁的page對象
    for i in page1:         #遍歷第1頁的全部數據對象
        print(i)

    print(page1.object_list) #第1頁的全部數據

    page2=paginator.page(2)

    print(page2.has_next())            #是否有下一頁
    print(page2.next_page_number())    #下一頁的頁碼
    print(page2.has_previous())        #是否有上一頁
    print(page2.previous_page_number()) #上一頁的頁碼

    # 拋錯
    #page=paginator.page(12)   # error:EmptyPage

    #page=paginator.page("z")   # error:PageNotAnInteger

    '''

 模板層

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
    <title>Title</title>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <table class="table">
                <tr>
                    <td>圖書</td>
                    <td>價格</td>
                </tr>
                {% for book in current_page %}  //根據後臺傳過來的當前頁面的數據進行打印
                    <tr>
                        <td>{{ book.name }}</td>
                        <td>{{ book.price }}</td>
                    </tr>
                {% endfor %}
            </table>
            <nav aria-label="Page navigation">
                <ul class="pagination">
                    {% if current_page.has_previous %}  //has_previous 進行判斷是否有上一頁
                        <li>
                            <a href="/index/?page={{ current_page.previous_page_number }}" aria-label="Previous">
                                <span aria-hidden="true">上一頁</span>
                            </a>
                        </li>
                    {% else %}
                        <li>
                            <a href="/index/?page={{ current_page_num }}" aria-label="Previous">
                                <span aria-hidden="true">上一頁</span>
                            </a>
                        </li>
                    {% endif %}

                    {% for foo in pagerange %}
                        {% if current_page_num == foo %}
                            <li class="active"><a href="/index/?page={{ foo }}">{{ foo }}</a></li>
                        {% else %}
                            <li><a href="/index/?page={{ foo }}">{{ foo }}</a></li>
                        {% endif %}
                    {% endfor %}
                    {% if current_page.has_next %}  //has_next 進行判斷是否有上一頁
                        <li>
                            <a href="/index/?page={{ current_page.next_page_number }}" aria-label="Next">
                                <span aria-hidden="true">下一頁</span>
                            </a>
                        </li>
                    {% else %}
                        <li>
                            <a href="/index/?page={{ current_page }}" aria-label="Next">
                                <span aria-hidden="true">下一頁</span>
                            </a>
                        </li>
                    {% endif %}
                </ul>
            </nav>
        </div>
    </div>
</div>
</body>
<script src="/static/jquery-3.3.1.js"></script>
<script src="/static/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
</html>
相關文章
相關標籤/搜索