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

Django 自定義分頁(yè)器的實(shí)現(xiàn)代碼

 更新時(shí)間:2019年11月24日 09:44:56   作者:Nolinked  
這篇文章主要介紹了Django 自定義分頁(yè)器的實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

為什么要實(shí)現(xiàn)分頁(yè)?

在大部分網(wǎng)站中分頁(yè)的功能都是必要的,尤其是在后臺(tái)管理中分頁(yè)更是不可或缺

分頁(yè)能帶給用戶(hù)更好的體驗(yàn),也能減輕服務(wù)器的壓力

對(duì)于分頁(yè)來(lái)說(shuō),有許多方法都可以實(shí)現(xiàn)

例如把數(shù)據(jù)全部讀取出來(lái)在前端用javascript實(shí)現(xiàn),但這樣一次請(qǐng)求全部數(shù)據(jù)服務(wù)器壓力很大,

還有就是在后端實(shí)現(xiàn),每一次請(qǐng)求部分?jǐn)?shù)據(jù)顯示

分頁(yè)需求:

1. 每頁(yè)顯示的多少條數(shù)據(jù)

2. 頁(yè)面顯示多少個(gè)頁(yè)碼

3. 上一頁(yè)和下一頁(yè)

4. 首頁(yè)和尾頁(yè)

效果演示:

代碼實(shí)現(xiàn):

分頁(yè)類(lèi)封裝:

在我的app下創(chuàng)建一個(gè)page.py文件,進(jìn)行封裝,我是先在我的app下創(chuàng)建了一個(gè)utils文件再創(chuàng)建page.py

class Pagination(object):

  def __init__(self, current_page_num, all_count, request, per_page_num=10, pager_count=11):
    """
    封裝分頁(yè)相關(guān)數(shù)據(jù)
    :param current_page_num: 當(dāng)前訪(fǎng)問(wèn)頁(yè)的數(shù)字
    :param all_count:  分頁(yè)數(shù)據(jù)中的數(shù)據(jù)總條數(shù)
    :param per_page_num: 每頁(yè)顯示的數(shù)據(jù)條數(shù)
    :param pager_count: 最多顯示的頁(yè)碼個(gè)數(shù)
    """
    try:
      current_page_num = int(current_page_num)
    except Exception as e:
      current_page_num = 1

    if current_page_num < 1:
      current_page_num = 1

    self.current_page_num = current_page_num

    self.all_count = all_count
    self.per_page_num = per_page_num

    # 實(shí)際總頁(yè)碼
    all_pager, tmp = divmod(all_count, per_page_num)
    if tmp:
      all_pager += 1
    self.all_pager = all_pager

    self.pager_count = pager_count
    self.pager_count_half = int((pager_count - 1) / 2) # 5

    # 保存搜索條件
    import copy
    self.params = copy.deepcopy(request.GET) # {"a":"1","b":"2"}

  # 開(kāi)始
  @property
  def start(self):
    return (self.current_page_num - 1) * self.per_page_num

  # 結(jié)束
  @property
  def end(self):
    return self.current_page_num * self.per_page_num

  # 實(shí)現(xiàn)
  def page_html(self):
    # 如果總頁(yè)碼 < 11個(gè):
    if self.all_pager <= self.pager_count:
      pager_start = 1
      pager_end = self.all_pager + 1
    # 總頁(yè)碼 > 11
    else:
      # 當(dāng)前頁(yè)如果<=頁(yè)面上最多顯示11/2個(gè)頁(yè)碼
      if self.current_page_num <= self.pager_count_half:
        pager_start = 1
        pager_end = self.pager_count + 1
      # 當(dāng)前頁(yè)大于5
      else:
        # 頁(yè)碼翻到最后
        if (self.current_page_num + self.pager_count_half) > self.all_pager:

          pager_start = self.all_pager - self.pager_count + 1
          pager_end = self.all_pager + 1

        else:
          pager_start = self.current_page_num - self.pager_count_half
          pager_end = self.current_page_num + self.pager_count_half + 1

    page_html_list = []

    first_page = '<li><a href="?page=%s" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁(yè)</a></li>' % (1,)
    page_html_list.append(first_page)

    if self.current_page_num <= 1:
      prev_page = '<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" >上一頁(yè)</a></li>'
    else:
      prev_page = '<li><a href="?page=%s" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上一頁(yè)</a></li>' % (self.current_page_num - 1,)

    page_html_list.append(prev_page)

    # self.params=copy.deepcopy(request.GET) # {"a":"1","b":"2"}

    for i in range(pager_start, pager_end):

      self.params["page"] = i

      if i == self.current_page_num:
        temp = '<li class="active"><a href="?%s" rel="external nofollow" rel="external nofollow" >%s</a></li>' % (self.params.urlencode(), i)
      else:
        temp = '<li><a href="?%s" rel="external nofollow" rel="external nofollow" >%s</a></li>' % (self.params.urlencode(), i,)
      page_html_list.append(temp)

    if self.current_page_num >= self.all_pager:
      next_page = '<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" >下一頁(yè)</a></li>'
    else:
      next_page = '<li><a href="?page=%s" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下一頁(yè)</a></li>' % (self.current_page_num + 1,)
    page_html_list.append(next_page)
    last_page = '<li><a href="?page=%s" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁(yè)</a></li>' % (self.all_pager,)
    page_html_list.append(last_page)

    return ''.join(page_html_list)

在視圖中使用

views.py

# 首先導(dǎo)入包
from myapp.utils.page import Pagination
from myapp.models import User


def index(request):
  # queryset
  user_list = User.objects.all()
  # 總頁(yè)數(shù)
  page_count = user_list.count()
  # 當(dāng)前頁(yè)
  current_page_num = request.GET.get("page")
  pagination = Pagination(current_page_num, page_count, request, per_page_num=1)
  # 處理之后的數(shù)據(jù)
  user_list = user_list[pagination.start:pagination.end]

  content = {
    "user_list": user_list,
    "pagination": pagination,
  }
  return render(request, "user_list.html", content)

頁(yè)面顯示

user_list.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>index</title>
  <link rel="stylesheet"  rel="external nofollow" >
</head>
<body>
<div class="container">
  <table class="table table-striped">
    <thead>
    <tr>
      <th>name</th>
    </tr>
    </thead>
    <tbody>
    {% for user in user_list %}
      <tr>
        <td>{{ user.name }}</td>
      </tr>
    {% endfor %}
    </tbody>
  </table>
  <!-- bootstrap 樣式 -->
  <div class="dataTables_paginate paging_simple_numbers pull-right">
    <ul class="pagination">
      {{ pagination.page_html|safe }}
    </ul>
  </div>
</div>
</body>
</html>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Mac 上切換Python多版本

    Mac 上切換Python多版本

    Mac上自帶了Python2.x的版本,有時(shí)需要使用Python3.x版本做開(kāi)發(fā),但不能刪了Python2.x,可能引起系統(tǒng)不穩(wěn)定,那么就需要安裝多個(gè)版本的Python下面通過(guò)本文給大家介紹Mac 上切換Python多版本的方法,需要的的朋友一起看看吧
    2017-06-06
  • Python?Matplotlib?marker?標(biāo)記詳解

    Python?Matplotlib?marker?標(biāo)記詳解

    這篇文章主要介紹了Python?Matplotlib?marker?標(biāo)記詳解,Matplotlib,風(fēng)格類(lèi)似?Matlab?的基于?Python?的圖表繪圖系統(tǒng),詳細(xì)內(nèi)容需要的小伙伴可以參考一下
    2022-07-07
  • Python中2種常用數(shù)據(jù)可視化庫(kù)Bokeh和Altair使用示例詳解

    Python中2種常用數(shù)據(jù)可視化庫(kù)Bokeh和Altair使用示例詳解

    本文對(duì)Python中兩個(gè)常用的數(shù)據(jù)可視化庫(kù)?Bokeh?和?Altair?進(jìn)行了比較和探討,通過(guò)對(duì)它們的特點(diǎn)、優(yōu)缺點(diǎn)以及使用示例的詳細(xì)分析,讀者可以更好地了解這兩個(gè)庫(kù)的功能和適用場(chǎng)景,從而更好地選擇合適的庫(kù)來(lái)進(jìn)行數(shù)據(jù)可視化工作,感興趣的朋友跟隨小編一起看看吧
    2024-04-04
  • Python基礎(chǔ)學(xué)習(xí)之深淺拷貝問(wèn)題及遞歸函數(shù)練習(xí)

    Python基礎(chǔ)學(xué)習(xí)之深淺拷貝問(wèn)題及遞歸函數(shù)練習(xí)

    在實(shí)際工作中,經(jīng)常涉及到數(shù)據(jù)的傳遞。這篇文章主要為大家介紹了Python的一些基礎(chǔ)學(xué)習(xí):深拷貝與淺拷貝問(wèn)題、遞歸函數(shù)的練習(xí),需要的朋友可以參考一下
    2021-12-12
  • 用python寫(xiě)的一個(gè)wordpress的采集程序

    用python寫(xiě)的一個(gè)wordpress的采集程序

    在學(xué)習(xí)python的過(guò)程中,經(jīng)過(guò)不斷的嘗試及努力,終于完成了第一個(gè)像樣的python程序,雖然還有很多需要優(yōu)化的地方,但是目前基本上實(shí)現(xiàn)了我所要求的功能,需要的朋友可以參考下
    2016-02-02
  • 使用Pandas計(jì)算系統(tǒng)客戶(hù)名稱(chēng)的相似度

    使用Pandas計(jì)算系統(tǒng)客戶(hù)名稱(chēng)的相似度

    在日常業(yè)務(wù)處理中,我們經(jīng)常會(huì)面臨將不同系統(tǒng)中的數(shù)據(jù)進(jìn)行匹配和比對(duì)的情況,本文將介紹如何使用Python的Pandas庫(kù)來(lái)處理這個(gè)問(wèn)題,需要的可以參考一下
    2023-07-07
  • Python __setattr__、 __getattr__、 __delattr__、__call__用法示例

    Python __setattr__、 __getattr__、 __delattr__、__call__用法示例

    這篇文章主要介紹了Python __setattr__、 __getattr__、 __delattr__、__call__用法示例,本文分別對(duì)這幾個(gè)魔法方法做了講解,需要的朋友可以參考下
    2015-03-03
  • NumPy進(jìn)行統(tǒng)計(jì)分析

    NumPy進(jìn)行統(tǒng)計(jì)分析

    本文主要介紹了NumPy進(jìn)行統(tǒng)計(jì)分析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • 深入探究Python中的迭代器和生成器

    深入探究Python中的迭代器和生成器

    迭代器(Iterators)和生成器(Generators)是?Python?中最強(qiáng)大的功能之一,但也是新手最容易混淆的部分,本文將深入探討這兩種概念,以及它們?cè)?Python?編程中的實(shí)際應(yīng)用,需要的朋友可以參考下
    2023-06-06
  • 使用Python輕松完成垃圾分類(lèi)(基于圖像識(shí)別)

    使用Python輕松完成垃圾分類(lèi)(基于圖像識(shí)別)

    這篇文章主要介紹了使用Python輕松完成垃圾分類(lèi)(基于圖像識(shí)別),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07

最新評(píng)論