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

Django框架 Pagination分頁(yè)實(shí)現(xiàn)代碼實(shí)例

 更新時(shí)間:2019年09月04日 10:13:25   作者:高~雅  
這篇文章主要介紹了Django框架 Pagination分頁(yè)實(shí)現(xiàn)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

一、自定義分頁(yè)

1、基礎(chǔ)版自定義分頁(yè)

data = []
 
for i in range(1, 302):
  tmp = {"id": i, "name": "alex-{}".format(i)}
  data.append(tmp)
 
print(data)
def user_list(request):
 
  # user_list = data[0:10]
  # user_list = data[10:20]
  try:
    current_page = int(request.GET.get("page"))
  except Exception as e:
    current_page = 1
 
  per_page = 10
 
  # 數(shù)據(jù)總條數(shù)
  total_count = len(data)
  # 總頁(yè)碼
  total_page, more = divmod(total_count, per_page)
  if more:
    total_page += 1
 
  # 頁(yè)面最多顯示多少個(gè)頁(yè)碼
  max_show = 11
  half_show = int((max_show-1)/2)
 
  if current_page <= half_show:
    show_start = 1
    show_end = max_show
  else:
    if current_page + half_show >= total_page:
      show_start = total_page - max_show
      show_end = total_page
    else:
      show_start = current_page - half_show
      show_end = current_page + half_show
 
  # 數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)
  data_start = (current_page - 1) * per_page
  data_end = current_page * per_page
 
  user_list = data[data_start:data_end]
 
  # 生成頁(yè)面上顯示的頁(yè)碼
  page_html_list = []
  # 加首頁(yè)
  first_li = '<li><a href="/user_list/?page=1" rel="external nofollow" >首頁(yè)</a></li>'
  page_html_list.append(first_li)
  # 加上一頁(yè)
  if current_page == 1:
    prev_li = '<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上一頁(yè)</a></li>'
  else:
    prev_li = '<li><a href="/user_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上一頁(yè)</a></li>'.format(current_page - 1)
  page_html_list.append(prev_li)
  for i in range(show_start, show_end+1):
    if i == current_page:
      li_tag = '<li class="active"><a href="/user_list/?page={0}" rel="external nofollow" rel="external nofollow" >{0}</a></li>'.format(i)
    else:
      li_tag = '<li><a href="/user_list/?page={0}" rel="external nofollow" rel="external nofollow" >{0}</a></li>'.format(i)
    page_html_list.append(li_tag)
 
  # 加下一頁(yè)
  if current_page == total_page:
    next_li = '<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下一頁(yè)</a></li>'
  else:
    next_li = '<li><a href="/user_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下一頁(yè)</a></li>'.format(current_page+1)
  page_html_list.append(next_li)
 
  # 加尾頁(yè)
  page_end_li = '<li><a href="/user_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁(yè)</a></li>'.format(total_page)
  page_html_list.append(page_end_li)
 
  page_html = "".join(page_html_list)
 
  return render(request, "user_list.html", {"user_list": user_list, "page_html": page_html})

2、封裝保存版

class Pagination(object):
  def __init__(self, current_page, total_count, base_url, per_page=10, max_show=11):
    """
    :param current_page: 當(dāng)前頁(yè)
    :param total_count: 數(shù)據(jù)庫(kù)中數(shù)據(jù)總數(shù)
    :param per_page: 每頁(yè)顯示多少條數(shù)據(jù)
    :param max_show: 最多顯示多少頁(yè)
    """
    try:
      current_page = int(current_page)
    except Exception as e:
      current_page = 1
 
    self.current_page = current_page
    self.total_count = total_count
    self.base_url = base_url
    self.per_page = per_page
    self.max_show = max_show
 
    # 總頁(yè)碼
    total_page, more = divmod(total_count, per_page)
    if more:
      total_page += 1
     
    half_show = int((max_show - 1) / 2)
    self.half_show = half_show
    self.total_page = total_page
 
  @property
  def start(self):
    return (self.current_page - 1) * self.per_page
 
  @property
  def end(self):
    return self.current_page * self.per_page
 
  def page_html(self):
 
    if self.current_page <= self.half_show:
      show_start = 1
      show_end = self.max_show
    else:
      if self.current_page + self.half_show >= self.total_page:
        show_start = self.total_page - self.max_show
        show_end = self.total_page
      else:
        show_start = self.current_page - self.half_show
        show_end = self.current_page + self.half_show
 
        # 生成頁(yè)面上顯示的頁(yè)碼
    page_html_list = []
    # 加首頁(yè)
    first_li = '<li><a href="{}?page=1" rel="external nofollow" >首頁(yè)</a></li>'.format(self.base_url)
    page_html_list.append(first_li)
    # 加上一頁(yè)
    if self.current_page == 1:
      prev_li = '<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上一頁(yè)</a></li>'
    else:
      prev_li = '<li><a href="{0}?page={1}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上一頁(yè)</a></li>'.format(self.base_url, self.current_page - 1)
    page_html_list.append(prev_li)
    for i in range(show_start, show_end + 1):
      if i == self.current_page:
        li_tag = '<li class="active"><a href="{0}?page={1}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{1}</a></li>'.format(self.base_url, i)
      else:
        li_tag = '<li><a href="{0}?page={1}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{1}</a></li>'.format(self.base_url, i)
      page_html_list.append(li_tag)
     # 加下一頁(yè)
    if self.current_page == self.total_page:
      next_li = '<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下一頁(yè)</a></li>'
    else:
      next_li = '<li><a href="{0}?page={1}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下一頁(yè)</a></li>'.format(self.base_url, self.current_page + 1)
    page_html_list.append(next_li)
 
    # 加尾頁(yè)
    page_end_li = '<li><a href="{0}?page={1}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁(yè)</a></li>'.format(self.base_url, self.total_page)
    page_html_list.append(page_end_li)
     return "".join(page_html_list)

3、封裝保存版使用指南

def user_list(request):
  pager = Pagination(request.GET.get("page"), len(data), request.path_info)
  user_list = data[pager.start:pager.end]
  page_html = pager.page_html()
  return render(request, "user_list.html", {"user_list": user_list, "page_html": page_html})

二、Django內(nèi)置分頁(yè)

1、內(nèi)置分頁(yè)view部分

from django.shortcuts import render
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
L = []
for i in range(999):
  L.append(i)
def index(request):
  current_page = request.GET.get('p')
  paginator = Paginator(L, 10)
  # per_page: 每頁(yè)顯示條目數(shù)量
  # count:  數(shù)據(jù)總個(gè)數(shù)
  # num_pages:總頁(yè)數(shù)
  # page_range:總頁(yè)數(shù)的索引范圍,如: (1,10),(1,200)
  # page:   page對(duì)象
  try:
    posts = paginator.page(current_page)
    # has_next       是否有下一頁(yè)
    # next_page_number   下一頁(yè)頁(yè)碼
    # has_previous     是否有上一頁(yè)
    # previous_page_number 上一頁(yè)頁(yè)碼
    # object_list      分頁(yè)之后的數(shù)據(jù)列表
    # number        當(dāng)前頁(yè)
    # paginator       paginator對(duì)象
  except PageNotAnInteger:
    posts = paginator.page(1)
  except EmptyPage:
    posts = paginator.page(paginator.num_pages)
  return render(request, 'index.html', {'posts': posts}) 

2、內(nèi)置分頁(yè)HTML部分

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
<ul>
  {% for item in posts %}
    <li>{{ item }}</li>
  {% endfor %}
</ul>
 
<div class="pagination">
   <span class="step-links">
    {% if posts.has_previous %}
      <a href="?p={{ posts.previous_page_number }}" rel="external nofollow" >Previous</a>
    {% endif %}
     <span class="current">
      Page {{ posts.number }} of {{ posts.paginator.num_pages }}.
     </span>
     {% if posts.has_next %}
       <a href="?p={{ posts.next_page_number }}" rel="external nofollow" >Next</a>
     {% endif %}
   </span>
</div>
</body>
</html>

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

相關(guān)文章

  • pandas將多個(gè)dataframe以多個(gè)sheet的形式保存到一個(gè)excel文件中

    pandas將多個(gè)dataframe以多個(gè)sheet的形式保存到一個(gè)excel文件中

    這篇文章主要介紹了pandas將多個(gè)dataframe以多個(gè)sheet的形式保存到一個(gè)excel文件中,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • python通過(guò)matplotlib生成復(fù)合餅圖

    python通過(guò)matplotlib生成復(fù)合餅圖

    這篇文章主要介紹了python通過(guò)matplotlib生成復(fù)合餅圖,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02
  • 詳解Python數(shù)據(jù)可視化編程 - 詞云生成并保存(jieba+WordCloud)

    詳解Python數(shù)據(jù)可視化編程 - 詞云生成并保存(jieba+WordCloud)

    這篇文章主要介紹了Python數(shù)據(jù)可視化編程 - 詞云生成并保存(jieba+WordCloud),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Python+opencv+pyaudio實(shí)現(xiàn)帶聲音屏幕錄制

    Python+opencv+pyaudio實(shí)現(xiàn)帶聲音屏幕錄制

    今天小編就為大家分享一篇Python+opencv+pyaudio實(shí)現(xiàn)帶聲音屏幕錄制,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • Python實(shí)現(xiàn)的批量修改文件后綴名操作示例

    Python實(shí)現(xiàn)的批量修改文件后綴名操作示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的批量修改文件后綴名操作,涉及Python目錄文件的遍歷、重命名等相關(guān)操作技巧,需要的朋友可以參考下
    2018-12-12
  • 如何使用Python在2秒內(nèi)評(píng)估國(guó)際象棋位置詳解

    如何使用Python在2秒內(nèi)評(píng)估國(guó)際象棋位置詳解

    關(guān)心編程語(yǔ)言的使用趨勢(shì)的人都知道,最近幾年,國(guó)內(nèi)最火的兩種語(yǔ)言非Python與Go莫屬,下面這篇文章主要給大家介紹了關(guān)于如何使用Python在2秒內(nèi)評(píng)估國(guó)際象棋位置的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • Python中AND、OR的一個(gè)使用小技巧

    Python中AND、OR的一個(gè)使用小技巧

    這篇文章主要介紹了Python中AND、OR的一個(gè)使用小技巧,需要的朋友可以參考下
    2015-02-02
  • Pandas中字符串和時(shí)間轉(zhuǎn)換與格式化的實(shí)現(xiàn)

    Pandas中字符串和時(shí)間轉(zhuǎn)換與格式化的實(shí)現(xiàn)

    本文主要介紹了Pandas中字符串和時(shí)間轉(zhuǎn)換與格式化的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Python的Flask框架中集成CKeditor富文本編輯器的教程

    Python的Flask框架中集成CKeditor富文本編輯器的教程

    在用Flask搭建網(wǎng)站時(shí)的后臺(tái)文章編輯器可以使用CKeditor,CKeditor所支持的文本樣式較多且開源,這里我們就來(lái)看一下Python的Flask框架中集成CKeditor富文本編輯器的教程
    2016-06-06
  • Django操作cookie的實(shí)現(xiàn)

    Django操作cookie的實(shí)現(xiàn)

    很多網(wǎng)站都會(huì)使用Cookie。本文主要介紹了Django操作cookie的實(shí)現(xiàn),結(jié)合實(shí)例形式詳細(xì)分析了Django框架針對(duì)cookie操作的各種常見技巧與操作注意事項(xiàng),需要的朋友可以參考下
    2021-05-05

最新評(píng)論

定结县| 甘谷县| 萝北县| 本溪市| 江华| 即墨市| 太谷县| 油尖旺区| 锡林浩特市| 榆树市| 东乌| 鄂托克旗| 彭水| 高尔夫| 靖宇县| 宝应县| 南阳市| 十堰市| 梅州市| 南康市| 朝阳市| 满城县| 射阳县| 连云港市| 金坛市| 彭山县| 怀来县| 民县| 东乡族自治县| 彰武县| 祁东县| 西城区| 肇州县| 蒙山县| 石台县| 昭觉县| 肇源县| 胶南市| 新兴县| 辽宁省| 莱阳市|