最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

對(duì)于分頁來說,有許多方法都可以實(shí)現(xiàn)

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

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

分頁需求:

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

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

3. 上一頁和下一頁

4. 首頁和尾頁

效果演示:

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

分頁類封裝:

在我的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):
    """
    封裝分頁相關(guān)數(shù)據(jù)
    :param current_page_num: 當(dāng)前訪問頁的數(shù)字
    :param all_count:  分頁數(shù)據(jù)中的數(shù)據(jù)總條數(shù)
    :param per_page_num: 每頁顯示的數(shù)據(jù)條數(shù)
    :param pager_count: 最多顯示的頁碼個(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í)際總頁碼
    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"}

  # 開始
  @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):
    # 如果總頁碼 < 11個(gè):
    if self.all_pager <= self.pager_count:
      pager_start = 1
      pager_end = self.all_pager + 1
    # 總頁碼 > 11
    else:
      # 當(dāng)前頁如果<=頁面上最多顯示11/2個(gè)頁碼
      if self.current_page_num <= self.pager_count_half:
        pager_start = 1
        pager_end = self.pager_count + 1
      # 當(dāng)前頁大于5
      else:
        # 頁碼翻到最后
        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" >首頁</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" >上一頁</a></li>'
    else:
      prev_page = '<li><a href="?page=%s" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上一頁</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" >下一頁</a></li>'
    else:
      next_page = '<li><a href="?page=%s" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下一頁</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" >尾頁</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()
  # 總頁數(shù)
  page_count = user_list.count()
  # 當(dāng)前頁
  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)

頁面顯示

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>

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

相關(guān)文章

最新評(píng)論

大姚县| 漠河县| 河池市| 当雄县| 抚远县| 凤凰县| 石楼县| 塔河县| 普格县| 松滋市| 庐江县| 无极县| 洛浦县| 阿图什市| 河西区| 鄯善县| 杭锦后旗| 镇巴县| 诸暨市| 赫章县| 锡林郭勒盟| 白河县| 麻城市| 辉县市| 如皋市| 岑巩县| 长子县| 望江县| 万宁市| 高雄市| 北票市| 兴山县| 白玉县| 昭苏县| 洛宁县| 铁岭县| 乌拉特中旗| 东安县| 河间市| 长海县| 瓦房店市|