欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

一、自定義分頁

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

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)
  # 總頁碼
  total_page, more = divmod(total_count, per_page)
  if more:
    total_page += 1
 
  # 頁面最多顯示多少個(gè)頁碼
  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]
 
  # 生成頁面上顯示的頁碼
  page_html_list = []
  # 加首頁
  first_li = '<li><a href="/user_list/?page=1" rel="external nofollow" >首頁</a></li>'
  page_html_list.append(first_li)
  # 加上一頁
  if current_page == 1:
    prev_li = '<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上一頁</a></li>'
  else:
    prev_li = '<li><a href="/user_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上一頁</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)
 
  # 加下一頁
  if current_page == total_page:
    next_li = '<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下一頁</a></li>'
  else:
    next_li = '<li><a href="/user_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下一頁</a></li>'.format(current_page+1)
  page_html_list.append(next_li)
 
  # 加尾頁
  page_end_li = '<li><a href="/user_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁</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)前頁
    :param total_count: 數(shù)據(jù)庫(kù)中數(shù)據(jù)總數(shù)
    :param per_page: 每頁顯示多少條數(shù)據(jù)
    :param max_show: 最多顯示多少頁
    """
    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
 
    # 總頁碼
    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
 
        # 生成頁面上顯示的頁碼
    page_html_list = []
    # 加首頁
    first_li = '<li><a href="{}?page=1" rel="external nofollow" >首頁</a></li>'.format(self.base_url)
    page_html_list.append(first_li)
    # 加上一頁
    if self.current_page == 1:
      prev_li = '<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上一頁</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" >上一頁</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)
     # 加下一頁
    if self.current_page == self.total_page:
      next_li = '<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下一頁</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" >下一頁</a></li>'.format(self.base_url, self.current_page + 1)
    page_html_list.append(next_li)
 
    # 加尾頁
    page_end_li = '<li><a href="{0}?page={1}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁</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)置分頁

1、內(nèi)置分頁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: 每頁顯示條目數(shù)量
  # count:  數(shù)據(jù)總個(gè)數(shù)
  # num_pages:總頁數(shù)
  # page_range:總頁數(shù)的索引范圍,如: (1,10),(1,200)
  # page:   page對(duì)象
  try:
    posts = paginator.page(current_page)
    # has_next       是否有下一頁
    # next_page_number   下一頁頁碼
    # has_previous     是否有上一頁
    # previous_page_number 上一頁頁碼
    # object_list      分頁之后的數(shù)據(jù)列表
    # number        當(dāng)前頁
    # 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)置分頁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)文章

  • 詳解PyTorch手寫數(shù)字識(shí)別(MNIST數(shù)據(jù)集)

    詳解PyTorch手寫數(shù)字識(shí)別(MNIST數(shù)據(jù)集)

    這篇文章主要介紹了詳解PyTorch手寫數(shù)字識(shí)別(MNIST數(shù)據(jù)集),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • python刪除過期文件的方法

    python刪除過期文件的方法

    這篇文章主要介紹了python刪除過期文件的方法,涉及Python日期與文件的相關(guān)操作技巧,需要的朋友可以參考下
    2015-05-05
  • tensorflow學(xué)習(xí)教程之文本分類詳析

    tensorflow學(xué)習(xí)教程之文本分類詳析

    初學(xué)tensorflow,借鑒了很多別人的經(jīng)驗(yàn),參考博客對(duì)評(píng)論分類(感謝博主的一系列好文),本人也嘗試著實(shí)現(xiàn)了對(duì)文本數(shù)據(jù)的分類,下面這篇文章主要給大家介紹了關(guān)于tensorflow學(xué)習(xí)教程之文本分類的相關(guān)資料,需要的朋友可以參考下
    2018-08-08
  • Python實(shí)現(xiàn)爬取并分析電商評(píng)論

    Python實(shí)現(xiàn)爬取并分析電商評(píng)論

    這篇文章主要介紹了Python實(shí)現(xiàn)爬取并分析電商評(píng)論,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Python求凸包及多邊形面積教程

    Python求凸包及多邊形面積教程

    這篇文章主要介紹了Python求凸包及多邊形面積教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python爬蟲scrapy圖書分類實(shí)例講解

    python爬蟲scrapy圖書分類實(shí)例講解

    在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于python爬蟲scrapy圖書分類實(shí)例講解內(nèi)容,需要的朋友們可以參考下。
    2020-11-11
  • Python中的復(fù)制、淺拷貝與深拷貝解讀

    Python中的復(fù)制、淺拷貝與深拷貝解讀

    這篇文章主要介紹了Python中的復(fù)制、淺拷貝與深拷貝解讀,對(duì)于可變對(duì)象,賦值是最簡(jiǎn)單省事的,如b=a,意思是直接使得a指向b代表的對(duì)象,兩者id一樣,指向同一個(gè)對(duì)象,一個(gè)修改,另一個(gè)也隨之變化,需要的朋友可以參考下
    2023-11-11
  • Django框架搭建的簡(jiǎn)易圖書信息網(wǎng)站案例

    Django框架搭建的簡(jiǎn)易圖書信息網(wǎng)站案例

    這篇文章主要介紹了Django框架搭建的簡(jiǎn)易圖書信息網(wǎng)站案例,結(jié)合具體實(shí)例形式分析了基于Django框架實(shí)現(xiàn)圖書信息管理網(wǎng)站的具體步驟、相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下
    2019-05-05
  • Django+Celery實(shí)現(xiàn)定時(shí)任務(wù)的示例

    Django+Celery實(shí)現(xiàn)定時(shí)任務(wù)的示例

    Celery是一個(gè)基于python開發(fā)的分布式任務(wù)隊(duì)列,而做python WEB開發(fā)最為流行的框架莫屬Django,本示例使用主要依賴包Django+Celery實(shí)現(xiàn)定時(shí)任務(wù),感興趣的朋友一起看看吧
    2021-06-06
  • python常用內(nèi)置模塊你了解嗎

    python常用內(nèi)置模塊你了解嗎

    這篇文章主要為大家介紹了python的常用內(nèi)置模塊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01

最新評(píng)論