1. 程式人生 > >Django分頁器(paginator)組件

Django分頁器(paginator)組件

插入 lin str 文件 row next 頁碼 ice nta

Django分頁器(paginator)組件

from django.core.paginator import Paginator
 from django.core.paginator import Paginator
    先拿到分頁器對象
        paginator = Paginator(book_list,3)   第一個參數:對象列表,第二個參數:每頁顯示的條數

    paginator.count 總條數
    paginator.num_pages  總頁數
    paginator.page_range  頁碼數列表

    current_page 
= paginator.page(5) 取某一頁,返回一個對象 current_page.object_list 某一頁裏所有數據,例如:這是第5也所有數據 current_page.has_next() 是否有下一頁 current_page.has_previous() 是否有上一頁 current_page.next_page_number() 下一頁的頁碼數 current_page,previous_page_number() 上一頁的頁碼數

分頁器邏輯分析

(1)批量插入 bulk_create
            li 
= [] for i in range(100): li.append(models.Book(name=圖書%s%i,price=10+i)) models.Book.objects.bulk_create(li) (2)分頁器分析: 先前臺傳過來哪一頁,後臺取哪一頁 current_page = paginator.page(5) 前臺可以循環當前頁的對象 paginator.page 也可以循環當前頁的內容 current_page.object_list 可以直接渲染出哪一頁的頁面內容 頁碼數分析:最大頁數小於
<11,就顯示最大頁數 最大頁數大於11時,再判斷我取的頁數 如果我取的頁數 小於 5 則 paginator.page_range 頁碼數列表 為 paginator.page_range(1,12) 如果我取的頁數 大於 最大頁數 則 paginator.page_range(paginator.num_pages-11,paginator.num_pages+1) 其他 則 get 發過來的頁碼數 +5 和 -5 current_range = range(current_page_num-5, current_page_num-5)

views 視圖層

from django.core.paginator import Paginator

def index(request):
    book_list = models.Book.objects.all()

    paginator = Paginator(book_list,3)   對所有數據進行分頁

    # page_range = paginator.page_range  # 頁碼數列表

    # print(page_range)

    try: # 捕捉前臺傳過來的數據,傳過來不正常的數據都跳到第一頁
        current_page_num = int(request.GET.get(page)) # 前臺傳過來的要拿一頁
        current_page = paginator.page(current_page_num)  # 拿哪一頁
        print(current_page.object_list)  # 拿哪一頁的所有數據

    #     # 這可以循環當前頁的對象 paginator.page   也可以循環當前頁的內容 current_page.object_list
    #     # for item in current_page:
    #     #     print(item.name)
    #
        if paginator.num_pages > 11:  # 判斷總頁數是否大於 11 頁
            if  current_page_num -5 < 1:
                current_range = range(1,12)
            elif current_page_num + 5 > paginator.num_pages:
                current_range = range(paginator.num_pages-10, paginator.num_pages+1)
            else: current_range = range(current_page_num-5, current_page_num-5)
        else:page_range = paginator.page_range
    #
    except Exception as e:
        current_page_num = 1
        current_page = paginator.page(current_page_num)
        current_range = range(1, 12)
    return render(request, index.html, locals())

Html文件 模板層

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
    <script src="/static/jquery-3.3.1.js"></script>
    <title>圖書列表</title>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <table class="table table-hover">
                <thead>
                <tr>
                    <th>書名</th>
                    <th>價格</th>
                </tr>
                </thead>
                <tbody>
                {% for book in  current_page %}
                    <tr>
                        <td>{{ book.name }}</td>
                        <td>價格{{ book.price }}</td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>


            <div>
                <nav aria-label="Page navigation">
                    <ul class="pagination">
                        {% if current_page.has_previous %}
                            <li>
                                <a href="/index/?page={{ current_page.previous_page_number }}" aria-label="Previous">
                                    <span aria-hidden="true">上一頁</span>
                                </a>
                            </li>
                        {% else %}
                            <li class="disabled">
                                <a href="" aria-label="Previous">
                                    <span aria-hidden="true">上一頁</span>
                                </a>
                            </li>
                        {% endif %}


                        {# 拿到頁碼書總列表 #}
                        {% for item in  page_range %}
                            {% if current_page_num == item %}
                                <li class="active"><a href="/index/?page={{ item }}">{{ item }}</a></li>
                            {% else %}
                                <li><a href="/index/?page={{ item }}">{{ item }}</a></li>

                            {% endif %}

                        {% endfor %}

                        {% if current_page.has_next %}
                            <li>
                                <a href="/index/?page={{ current_page.next_page_number }}" aria-label="Next">
                                    <span aria-hidden="true">下一頁</span>
                                </a>
                            </li>
                        {% else %}
                            <li class="disabled">
                                <a href="" aria-label="Previous">
                                    <span aria-hidden="true">下一頁</span>
                                </a>
                            </li>

                        {% endif %}

                    </ul>
                </nav>
            </div>


        </div>
    </div>
</div>
</body>
</html>

Django分頁器(paginator)組件