13_分頁器

weixin_34253539發表於2018-11-20

分頁器

使用方法:

# 匯入
from django.core.paginator import Paginator
# 在檢視函式中
def test(request):
    book_list = Book.objects.all() # 所有書籍物件
    
    # 例項化出一個物件,第一個引數是要分頁的書籍物件,第二個引數是每頁多少條資料
    paginator = Paginator(book_list,10)
    
    # -----------------------------物件內的屬性----------------------------
    
    # 資料總條數--100條
    print(paginator.count)
    
    # 總頁數--10頁
    print(paginator.num_pages)
    
    # 頁碼數的列表
    print(paginator.page_range)
    
    # 取到第 x 頁 ,返回一個Page物件
    current_page=paginator.page(5)
    
    # 當前頁碼內所有的資料
    print(current_page.object_list)
    
    # 是否有下一頁
    print(current_page.has_next())
    # 是否有上一頁
    print(current_page.has_previous())
    
    # 下一頁頁碼數
    print(current_page.next_page_number())
    # 上一頁的頁碼數
    print(current_page.previous_page_number())

簡單小案例:

檢視函式頁面
# views.py
def test(request):
    book_list = models.Book.objects.all()
    paginator = Paginator(book_list, 5)

    try:
        # 通過GET請求方式取出當前頁碼數
        current_page_num = int(request.GET.get('page'))

        # 通過當前頁碼數,返回一個page物件
        current_page = paginator.page(current_page_num)
        
        if paginator.num_pages > 11:
            if current_page_num - 5 < 1:
                page_range = range(1, 12)
            elif current_page_num + 5 > paginator.num_pages:
                page_range = range(paginator.num_pages - 10, paginator.num_pages + 1)
            else:
                page_range = range(current_page_num - 5, current_page_num + 6)
        else:
            page_range = paginator.page_range
    except Exception as e:
        current_page_num = 1
    return render(request, 'test.html', locals())
html頁面
<!-- html頁面 -->
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <script src="/static/jquery-3.3.1.js"></script>
    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
    <title>圖書</title>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">

            <nav aria-label="Page navigation">
                <ul class="pagination">
                    {% if current_page.has_previous %}
                        <li>
                            <a href="/test/?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 foo in page_range %}
                        {% if current_page_num == foo %}
                            <li class="active"><a href="/test/?page={{ foo }}">{{ foo }}</a></li>
                        {% else %}
                            <li><a href="/test/?page={{ foo }}">{{ foo }}</a></li>
                        {% endif %}
                    {% endfor %}


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

                </ul>
            </nav>

            <table class="table table-striped">
                <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>
    </div>
</div>
</body>
</html>

HTML頁面講解:

<table class="table table-striped">
    <thead>
        <tr>
            <th>書籍名</th>
            <th>書籍價格</th>
        </tr>
    </thead>
    <tbody>
        <!-- current_page 是當前頁所包含的書籍物件,從後臺傳過來 -->
        {% for book in current_page %}
        <tr>
            <td>{{ book.name }}</td>
            <td>{{ book.price }}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>
上下頁:
<!-- 下一頁與上一頁相同,判斷是否有下一頁 -->
<!-- 判斷是否有上一頁,如果沒有,禁用上一頁,並且將 a標籤 裡的 href 清空 -->
{% if current_page.has_previous %}
<li>
    <a href="/test/?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 %}
<!-- page_range指的是顯示區間,比如當前頁為9,顯示左5右5,顯示區間就是range(4,15),顧頭不顧尾 -->
{% for foo in page_range %}
    <!-- 如果當前頁數與遍歷出的頁數相等,則該頁數為活躍狀態,新增 active -->
    {% if current_page_num == foo %}
        <li class="active"><a href="/test/?page={{ foo }}">{{ foo }}</a></li>
    {% else %}
        <li><a href="/test/?page={{ foo }}">{{ foo }}</a></li>
    {% endif %}
{% endfor %}

==================================================================

分頁器模板
<nav aria-label="Page navigation">
    <ul class="pagination">
        {% if current_page.has_previous %}
            <li>
                <a href="/?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 foo in page_range %}
            {% if current_page_num == foo %}
                <li class="active"><a href="/?page={{ foo }}">{{ foo }}</a></li>
            {% else %}
                <li><a href="/?page={{ foo }}">{{ foo }}</a></li>
            {% endif %}
        {% endfor %}



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

    </ul>
</nav>
後臺程式碼
 # 分頁器
        paginator = Paginator(article_list, 3)
        try:
            current_page_num = int(request.GET.get('page'))
            current_page = paginator.page(current_page_num)
        except Exception as e:
            current_page_num = 1
            current_page = paginator.page(current_page_num)
        if paginator.num_pages > 11:
            if current_page_num - 5 < 1:
                page_range = range(1, 12)
            elif current_page_num + 5 > paginator.num_pages:
                page_range = range(paginator.num_pages - 10, paginator.num_pages + 1)
            else:
                page_range = range(current_page_num - 5, current_page_num + 6)
        else:
            page_range = paginator.page_range

相關文章