1. 程式人生 > 其它 >Django分頁器

Django分頁器

from django.shortcuts import render
from .models import *
from django.core.paginator import Paginator, EmptyPage


def page(request):
    # 拿到資料庫queryset物件
    book_queryset = Book.objects.all()
    # 每頁10條資料,並將資料庫queryset物件傳入
    """類例項化,paginator_set物件方法有count/num_pages/page_range,都不需要加括號執行"""
    paginator_set 
= Paginator(book_queryset, 10) # 獲取前端傳送過來的page值,如果沒有page引數(第一次進入頁面時)或page引數為空,那麼預設為1 page_num = int(request.GET.get("page", 1)) # 如果page_num值不在總頁數之間,那麼paginator_set.page()會報EmptyPage錯 try: """類例項化,物件中包含每一頁10條書籍的物件,物件方法有has_next()/has_previous()/next_page_number()/previous_page_number()/start_index()/end_index()
""" current_page = paginator_set.page(page_num) except EmptyPage as e: current_page = paginator_set.page(1) # 如果總頁數大於12,那麼按照下面的邏輯進行判斷 if paginator_set.num_pages > 12: # 當前頁減去5小於1,那麼頁數維持在(1, 11) if page_num-5 < 1: page_range = range(1, 12)
# 當前頁加上5大於總頁數,那麼頁數維持在(總頁數-10, 尾頁+1) elif page_num+5 > paginator_set.num_pages: page_range = range(paginator_set.num_pages-10, paginator_set.num_pages+1) # 當前頁-5不小於1而且+5不大於總頁數,那麼頁數維持在(當前頁-5, 當前頁+6) else: page_range = range(page_num-5, page_num+6) # 如果總頁數小於12,那麼頁數和總頁數一樣,不做任何邏輯判斷 else: page_range = paginator_set.page_range return render(request, "page.html", locals())
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/static/bootstrap-3.4.1-dist/css/bootstrap.min.css">
    <script src="/static/bootstrap-3.4.1-dist/js/bootstrap.min.js"></script>
    <script src="/static/jQuery3.6.js"></script>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <table class="table table-bordered table-striped">
                <thead>
                <tr>
                    <th>序號</th>
                    <th>書籍名稱</th>
                    <th>書籍價格</th>
                </tr>
                </thead>
                <tbody>
                {# 迴圈paginator_set.page()例項化的物件,可以取到每一個的書籍物件 #}
                {% for current in current_page %}
                    {# 如果是每一頁第一條資料, 那麼序號則展示其start_index,start_index()返回值的計算方法是:(分頁條數*(當前頁-1))+1 #}
                    {% if current.first %}
                        <tr>
                            <td>{{ current_page.start_index }}</td>
                            <td>{{ current.title }}</td>
                            <td>{{ current.price }}</td>
                        </tr>
                    {# 如果不是每一頁第一條資料,那麼利用start_index()方法計算的值加counter0的值,結果範圍為(start_index()+1,start_index()+9) #}
                    {% else %}
                        <tr>
                            <td>{{ forloop.counter0|add:current_page.start_index }}</td>
                            <td>{{ current.title }}</td>
                            <td>{{ current.price }}</td>
                        </tr>
                    {% endif %}
                {% endfor %}
                </tbody>
            </table>

            <div class="text-center">
                <nav aria-label="Page navigation">
                    <ul class="pagination">
                        {# 首頁按鈕,固定page=1 #}
                        <li>
                            <a href="?page=1" aria-label="Previous">
                                <span aria-hidden="true">首頁</span>
                            </a>
                        </li>

                        {# 如果上一頁還有資料,那麼讓其可繼續點選進入上一頁 #}
                        {% if current_page.has_previous %}
                            <li>
                                <a href="?page={{ current_page.previous_page_number }}" aria-label="Previous">
                                    <span aria-hidden="true">&laquo;</span>
                                </a>
                            </li>
                        {# 如果上一頁沒有資料了,那麼不讓做任何操作 #}
                        {% else %}
                            <li class="disabled">
                                <span aria-hidden="true">&laquo;</span>
                            </li>
                        {% endif %}

                        {# 由後端經邏輯判斷後的page_range頁數列表進行迴圈 #}
                        {% for page in page_range %}
                            {# 如果迴圈的頁碼與當前檢視的頁碼相等,那麼就讓其高亮顯示 #}
                            {% if page == page_num %}
                                <li class="active"><a href="?page={{ page }}">{{ page }}</a></li>
                            {% else %}
                                <li><a href="?page={{ page }}">{{ page }}</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">&raquo;</span>
                                </a>
                            </li>
                        {# 如果當前頁沒有下一頁了,那麼不讓做任何操作 #}
                        {% else %}
                            <li class="disabled">
                                <span aria-hidden="true">&raquo;</span>
                            </li>
                        {% endif %}

                        {# 跳轉到尾頁,讓page引數直接等於總頁數 #}
                        <li>
                            <a href="?page={{ paginator_set.num_pages }}" aria-label="Previous">
                                <span aria-hidden="true">尾頁</span>
                            </a>
                        </li>
                    </ul>
                </nav>
            </div>
        </div>
    </div>
</div>
</body>
</html>

ORM批量插入資料方法推薦

from .models import *
book_list = []
for i in range(100):
    book_obj = Book(
        title="book_%s" % i,
        price=i * i,
    )
    book_list.append(book_obj)
Book.objects.bulk_create(book_list)
while True: print('studying...')