亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

django中的自定義分頁器的實(shí)現(xiàn)示例

 更新時(shí)間:2022年08月15日 08:11:04   作者:等日落  
本文主要介紹了django中的自定義分頁器的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1.什么是自定義分頁器

當(dāng)我們需要在前端頁面展示的數(shù)據(jù)太多的時(shí)候,我們總不能將數(shù)據(jù)展示在一頁上面吧!這時(shí),我們就需要自定義一個(gè)分頁器,將數(shù)據(jù)分成特定的頁數(shù)進(jìn)行展示,每一頁展示固定條數(shù)的數(shù)據(jù)!

2.為什么要用自定義分頁器

如上所說:為了將數(shù)據(jù)分成多頁進(jìn)行展示,分別閱讀,方便查詢!

3.如何使用自定義分頁器

3.1 自定義分頁器推導(dǎo)過程

雖然!我們有一個(gè)封裝好的分頁器源碼,用的時(shí)候只需要cv大法就行,但是作為一名優(yōu)秀的程序猿??!我們還是需要知道底層的邏輯是不是!

我們需要明確的是,前端向后端請(qǐng)求的常用方式為get和post請(qǐng)求。分頁的時(shí)候,我們應(yīng)該采用get請(qǐng)求的方式給后端傳輸您需要轉(zhuǎn)到的頁數(shù)?。?/p>

其次我們還需要知道一個(gè)點(diǎn),queryset對(duì)象是支持索引取值和切片操作的,但是不支持負(fù)數(shù)索引情況。

接下來,我們來推導(dǎo)一下分頁器的形成的邏輯:

current_page = request.GET.get("page",1)  # 獲取用戶想訪問的頁碼  如果沒有 默認(rèn)展示第一頁
try:  # 由于后端接受到的前端數(shù)據(jù)是字符串類型所以我們這里做類型轉(zhuǎn)換處理加異常捕獲
  current_page = int(current_page)
except Exception as e:
  current_page = 1
# 還需要定義頁面到底展示幾條數(shù)據(jù)
per_page_num = 10  # 一頁展示10條數(shù)據(jù)

# 需要對(duì)總數(shù)據(jù)進(jìn)行切片操作 需要確定切片起始位置和終止位置
start_page = ? 
end_page = ?
"""
下面需要研究current_page、per_page_num、start_page、end_page四個(gè)參數(shù)之間的數(shù)據(jù)關(guān)系
per_page_num = 10
current_page                start_page                  end_page
    1                           0                           10
    2                           10                          20
    3                           20                          30  
    4                           30                          40

per_page_num = 5
current_page                start_page                  end_page
    1                           0                           5
    2                           5                           10
    3                           10                          15  
    4                           15                          20
可以很明顯的看出規(guī)律
start_page = (current_page - 1) * per_page_num
end_page =  current_page* per_page_num
"""

我們研究完當(dāng)前頁(current_page)、每頁展示的數(shù)據(jù)條數(shù)(per_page_num)、每頁數(shù)據(jù)的起始位置(start_page)和結(jié)束位置(end_page)之后,我們還需要知道的最重要的一點(diǎn)是:

一共需要從數(shù)據(jù)庫取出的數(shù)據(jù)一共有多少條!??!

此時(shí),我們就需要用到python中的一個(gè)內(nèi)置方法divmod:它是功能是一個(gè)數(shù)除以另一個(gè)數(shù)時(shí),返回余數(shù)和商?。∪纾?/p>

>>> divmod(100,10)
(10, 0)  # 10頁
>>> divmod(101,10)
(10, 1)  # 11頁
>>> divmod(99,10)
(9, 9)  # 10頁
# 余數(shù)只要不是0就需要在第一個(gè)數(shù)字上加一

我們可以用它來判斷我們一共需要多少頁!

后端自定義分頁器邏輯詳解:

def book(request):
    if request.method == 'GET':
        current_page = request.GET.get('page',1) # 獲取用戶需要訪問的頁面,如果沒有默認(rèn)返回1
    try:   #異常捕獲,因?yàn)榍岸朔祷氐亩际亲址?,需要把他們都轉(zhuǎn)成數(shù)字類型,方便下面做運(yùn)算操作
        current_page = int(current_page)
    except Exception as e:
        current_page = 1  # 用戶輸入啥字母等也默認(rèn)為1
    per_page_num = 10  #每頁展示多少條數(shù)據(jù)
    start_page = (current_page - 1) * per_page_num  #當(dāng)前頁數(shù)起始數(shù)據(jù)
    end_page = current_page * per_page_num  #當(dāng)前頁結(jié)束數(shù)據(jù)
    book_num = models.Book.objects.all() #將所有數(shù)據(jù)查詢出來
    all_count = book_num.count()  # 統(tǒng)計(jì)一共有多少數(shù)據(jù)
    num,more = divmod(all_count,per_page_num)  #divmod方法計(jì)算需要的總頁數(shù)
    if more:
        all_page = num + 1 #more為余數(shù),余為0,則剛剛好是num頁數(shù),不為0,則頁數(shù)加1
    # 然后我們需要在html頁面的分頁器標(biāo)簽部分,for循環(huán)一下總共需要的num頁數(shù),但是有一個(gè)問題是,前端無法使用range()
    # 這就需要我們?cè)诤蠖诉M(jìn)行循環(huán),再傳到前端
    html = ''
    a = current_page # 為了下面頁碼高亮調(diào)整
    if current_page <6:  #當(dāng)頁面小于6時(shí),固定在6上面,為下面的for處理不出現(xiàn)頁碼負(fù)數(shù)
        current_page = 6
    # 訪問第6頁時(shí),只會(huì)顯示當(dāng)前頁-5和+6的底部頁碼數(shù),但是當(dāng)頁面小于6時(shí),頁碼會(huì)出現(xiàn)0甚至負(fù)數(shù),所有我們需要對(duì)頁面進(jìn)行一個(gè)設(shè)置,就是上述的a
    for i in range(current_page-5,current_page+6):
        if a == i:
            # 當(dāng)前頁的頁面高亮顯示
            html += '<li class="active"><a href="?page=%s" rel="external nofollow"  rel="external nofollow" >%s</a></li>' % (i, i)
        else:
            html += '<li><a href="?page=%s" rel="external nofollow"  rel="external nofollow" >%s</a></li>' % (i, i)
    book_queryset = book_num[start_page:end_page]
    return render(request,'book.html',locals())

前端頁面部分:
<nav aria-label="Page navigation">
  <ul class="pagination">
    <li>
      <a href="#" rel="external nofollow"  rel="external nofollow"  aria-label="Previous">
        <span aria-hidden="true">&laquo;</span>
      </a>
    </li>
        {{ html|safe }}
    <li>
      <a href="#" rel="external nofollow"  rel="external nofollow"  aria-label="Next">
        <span aria-hidden="true">&raquo;</span>
      </a>
    </li>
  </ul>
</nav>

3.2 自定義分頁器封裝代碼

封裝好的分液器

3.3 封裝好分頁器的使用

后端

 def get_book(request):
   book_list = models.Book.objects.all()
   current_page = request.GET.get("page",1)
   all_count = book_list.count()
   page_obj = Pagination(current_page=current_page,all_count=all_count,per_page_num=10)
   page_queryset = book_list[page_obj.start:page_obj.end]
   return render(request,'booklist.html',locals())

前端

<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            {% for book in page_queryset %}
            <p>{{ book.title }}</p>
            {% endfor %}
            {{ page_obj.page_html|safe }}
        </div>
    </div>
</div>

 到此這篇關(guān)于django中的自定義分頁器的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)django 自定義分頁器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論