1. 程式人生 > >django -----分頁器元件 分頁器元件

django -----分頁器元件 分頁器元件

分頁器元件

本文目錄

回到目錄

1 Django的分頁器(paginator)簡介

在頁面顯示分頁資料,需要用到Django分頁器元件

from django.core.paginator import Paginator

複製程式碼
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物件
複製程式碼

 

回到目錄

2 應用View層

複製程式碼
from django.shortcuts import render,HttpResponse

# Create your views here.
from app01.models import *
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def index(request): ''' 批量匯入資料: Booklist=[] for i in range(100): Booklist.append(Book(title="book"+str(i),price=30+i*i)) Book.objects.bulk_create(Booklist) ''' ''' 分頁器的使用: 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 ''' book_list=Book.objects.all() paginator = Paginator(book_list, 10) page = request.GET.get('page',1) currentPage=int(page) try: print(page) book_list = paginator.page(page) except PageNotAnInteger: book_list = paginator.page(1) except EmptyPage: book_list = paginator.page(paginator.num_pages) return render(request,"index.html",{"book_list":book_list,"paginator":paginator,"currentPage":currentPage})
複製程式碼 回到目錄

3 模版層 index.html

複製程式碼
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" 
    integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>

<div class="container">

    <h4>分頁器</h4>
    <ul>

        {% for book in book_list %}
             <li>{{ book.title }} -----{{ book.price }}</li>
        {% endfor %}

     </ul>


    <ul class="pagination" id="pager">

                 {% if book_list.has_previous %}
                    <li class="previous"><a href="/index/?page={{ book_list.previous_page_number }}">上一頁</a></li>
                 {% else %}
                    <li class="previous disabled"><a href="#">上一頁</a></li>
                 {% endif %}


                 {% for num in paginator.page_range %}

                     {% if num == currentPage %}
                       <li class="item active"><a href="/index/?page={{ num }}">{{ num }}</a></li>
                     {% else %}
                       <li class="item"><a href="/index/?page={{ num }}">{{ num }}</a></li>

                     {% endif %}
                 {% endfor %}



                 {% if book_list.has_next %}
                    <li class="next"><a href="/index/?page={{ book_list.next_page_number }}">下一頁</a></li>
                 {% else %}
                    <li class="next disabled"><a href="#">下一頁</a></li>
                 {% endif %}

            </ul>
</div>



</body>
</html>
複製程式碼 回到目錄

4 擴充套件

'''
    顯示左5,右5,總共11個頁,
    1 如果總頁碼大於11
        1.1 if 當前頁碼減5小於1,要生成1到12的列表(顧頭不顧尾,共11個頁碼)
            page_range=range(1,12)
        1.2 elif 當前頁碼+5大於總頁碼,生成當前頁碼減10,到當前頁碼加1的列表(顧頭不顧尾,共11個頁碼)
            page_range=range(paginator.num_pages-10,paginator.num_pages+1)
        1.3 else 生成當前頁碼-5,到當前頁碼+6的列表
            page_range=range(current_page_num-5,current_page_num+6)
    2 其它情況,生成的列表就是pageinator的page_range
        page_range=paginator.page_range

    '''
核心邏輯

 

複製程式碼
def index(request):


    book_list=Book.objects.all()

    paginator = Paginator(book_list, 15)
    page = request.GET.get('page',1)
    currentPage=int(page)

    #  如果頁數十分多時,換另外一種顯示方式
if paginator.num_pages>11: if currentPage-5<1: pageRange=range(1,11) elif currentPage+5>paginator.num_pages: pageRange=range(currentPage-5,paginator.num_pages+1) else: pageRange=range(currentPage-5,currentPage+5) else: pageRange=paginator.page_range try: print(page) book_list = paginator.page(page) except PageNotAnInteger: book_list = paginator.page(1) except EmptyPage: book_list = paginator.page(paginator.num_pages) return render(request,"index.html",locals())
複製程式碼

 

 

def page_test(request):
    # book_list=[]
    # for i in range(100):
    #     book=Book(name='book%s'%i,price=10+i,pub_date='2018-09-18',publish_id=1)
    #     book_list.append(book)
    # Book.objects.bulk_create(book_list,10)
    book_list=Book.objects.all()
    # 生成paginator物件,傳入書籍列表,每頁10條資料
    paginator=Paginator(book_list,3)
    # 總頁碼數
    print(paginator.num_pages)
    # 頁碼列表
    print(paginator.page_range)
    # 總資料
    print(paginator.count)
    # 獲取頁面傳來的頁碼
    current_page=int(request.GET.get('page',1))
    page_range=[]
    # 左5 右5

    # 獲取頁面傳來的頁碼的page物件
    try:
        page=paginator.page(current_page)
        # print(page.has_next())            #是否有下一頁
        # print(page.next_page_number())    #下一頁的頁碼
        # print(page.has_previous())        #是否有上一頁
        # print(page.previous_page_number()) #上一頁的頁碼
        # 迴圈打印出當頁物件
        for i in page:
            print(i)
    except Exception as e:
        current_page=1
        page = paginator.page(1)
    if paginator.num_pages>11:
        if current_page+5>paginator.num_pages:
            page_range=range(paginator.num_pages-10,paginator.num_pages+1)
        elif current_page-5<1:
            page_range=range(1,12)
        else:
            page_range=range(current_page-5,current_page+6)
    else:
        page_range=paginator.page_range



    return render(request,'page_test.html',locals())
views
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css">
    <title>Title</title>
</head>
<body>

<ul>

    {% for foo in page %}
        <li>{{ foo.name }}</li>

    {% endfor %}

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

        {% endfor %}
        {% if page.has_next %}
            <li>
                <a href="/page_test/?page={{ 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>


</body>
</html>
模版

 ---------https://www.cnblogs.com/liuqingzheng/articles/9509767.html

回到目錄

1 Django的分頁器(paginator)簡介

在頁面顯示分頁資料,需要用到Django分頁器元件

from django.core.paginator import Paginator

複製程式碼
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物件
複製程式碼

 

回到目錄

2 應用View層

複製程式碼
from django.shortcuts import render,HttpResponse

# Create your views here.
from app01.models import *
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def index(request):

    '''
    批量匯入資料:

    Booklist=[]
    for i in range(100):
        Booklist.append(Book(title="book"+str(i),price=30+i*i))
    Book.objects.bulk_create(Booklist)
    '''

    '''
分頁器的使用:

    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

    '''


    book_list=Book.objects.all()

    paginator = Paginator(book_list, 10)
    page = request.GET.get('page',1)
    currentPage=int(page)


    try:
        print(page)
        book_list = paginator.page(page)
    except PageNotAnInteger:
        book_list = paginator.page(1)
    except EmptyPage:
        book_list = paginator.page(paginator.num_pages)


    return render(request,"index.html",{"book_list":book_list,"paginator":paginator,"currentPage":currentPage})
複製程式碼 回到目錄

3 模版層 index.html

複製程式碼
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" 
    integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>

<div class="container">

    <h4>分頁器</h4>
    <ul>

        {% for book in book_list %}
             <li>{{ book.title }} -----{{ book.price }}</li>
        {% endfor %}

     </ul>


    <ul class="pagination" id="pager">

                 {% if book_list.has_previous %}
                    <li class="previous"><a href="/index/?page={{ book_list.previous_page_number }}">上一頁</a></li>
                 {% else %}
                    <li class="previous disabled"><a href="#">上一頁</a></li>
                 {% endif %}


                 {% for num in paginator.page_range %}

                     {% if num == currentPage %}
                       <li class="item active"><a href="/index/?page={{ num }}">{{ num }}</a></li>
                     {% else %}
                       <li class="item"><a href="/index/?page={{ num }}">{{ num }}</a></li>

                     {% endif %}
                 {% endfor %}



                 {% if book_list.has_next %}
                    <li class="next"><a href="/index/?page={{ book_list.next_page_number }}">下一頁</a></li>
                 {% else %}
                    <li class="next disabled"><a href="#">下一頁</a></li>
                 {% endif %}

            </ul>
</div>



</body>
</html>
複製程式碼 回到目錄

4 擴充套件

'''
    顯示左5,右5,總共11個頁,
    1 如果總頁碼大於11
        1.1 if 當前頁碼減5小於1,要生成1到12的列表(顧頭不顧尾,共11個頁碼)
            page_range=range(1,12)
        1.2 elif 當前頁碼+5大於總頁碼,生成當前頁碼減10,到當前頁碼加1的列表(顧頭不顧尾,共11個頁碼)
            page_range=range(paginator.num_pages-10,paginator.num_pages+1)
        1.3 else 生成當前頁碼-5,到當前頁碼+6的列表
            page_range=range(current_page_num-5,current_page_num+6)
    2 其它情況,生成的列表就是pageinator的page_range
        page_range=paginator.page_range

    '''
核心邏輯

 

複製程式碼
def index(request):


    book_list=Book.objects.all()

    paginator = Paginator(book_list, 15)
    page = request.GET.get('page',1)
    currentPage=int(page)

    #  如果頁數十分多時,換另外一種顯示方式
if paginator.num_pages>11: if currentPage-5<1: pageRange=range(1,11) elif currentPage+5>paginator.num_pages: pageRange=range(currentPage-5,paginator.num_pages+1) else: pageRange=range(currentPage-5,currentPage+5) else: pageRange=paginator.page_range try: print(page) book_list = paginator.page(page) except PageNotAnInteger: book_list = paginator.page(1) except EmptyPage: book_list = paginator.page(paginator.num_pages) return render(request,"index.html",locals())
複製程式碼

 

 

def page_test(request):
    # book_list=[]
    # for i in range(100):
    #     book=Book(name='book%s'%i,price=10+i,pub_date='2018-09-18',publish_id=1)
    #     book_list.append(book)
    # Book.objects.bulk_create(book_list,10)
    book_list=Book.objects.all()
    # 生成paginator物件,傳入書籍列表,每頁10條資料
    paginator=Paginator(book_list,3)
    # 總頁碼數
    print(paginator.num_pages)
    # 頁碼列表
    print(paginator.page_range)
    # 總資料
    print(paginator.count)
    # 獲取頁面傳來的頁碼
    current_page=int(request.GET.get('page',1))
    page_range=[]
    # 左5 右5

    # 獲取頁面傳來的頁碼的page物件
    try:
        page=paginator.page(current_page)
        # print(page.has_next())            #是否有下一頁
        # print(page.next_page_number())    #下一頁的頁碼
        # print(page.has_previous())        #是否有上一頁
        # print(page.previous_page_number()) #上一頁的頁碼
        # 迴圈打印出當頁物件
        for i in page:
            print(i)
    except Exception as e:
        current_page=1
        page = paginator.page(1)
    if paginator.num_pages>11:
        if current_page+5>paginator.num_pages:
            page_range=range(paginator.num_pages-10,paginator.num_pages+1)
        elif current_page-5<1:
            page_range=range(1,12)
        else:
            page_range=range(current_page-5,current_page+6)
    else:
        page_range=paginator.page_range



    return render(request,'page_test.html',locals())
views
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css">
    <title>Title</title>
</head>
<body>

<ul>

    {% for foo in page %}
        <li>{{ foo.name }}</li>

    {% endfor %}

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

        {% endfor %}
        {% if page.has_next %}
            <li>
                <a href="/page_test/?page={{ 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>


</body>
</html>
模版

 ---------https://www.cnblogs.com/liuqingzheng/articles/9509767.html