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

Django自定義分頁效果

 更新時(shí)間:2017年06月27日 08:51:39   作者:jack-boy  
這篇文章主要為大家詳細(xì)介紹了Django自定義分頁效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

分頁功能在每個(gè)網(wǎng)站都是必要的,對于分頁來說,其實(shí)就是根據(jù)用戶的輸入計(jì)算出應(yīng)該顯示在頁面上的數(shù)據(jù)在數(shù)據(jù)庫表中的起始位置。

確定分頁需求:

1. 每頁顯示的數(shù)據(jù)條數(shù)
2. 每頁顯示頁號鏈接數(shù)
3. 上一頁和下一頁
4. 首頁和末頁

效果圖:

首先,利用django內(nèi)置的分頁功能,寫分頁類:

from django.core.paginator import Paginator, Page  # 導(dǎo)入django分頁模塊


class PageInfo(object):
 def __init__(self, current_page, all_count, base_url, per_page=10, show_page=11):
  """

  :param current_page: 當(dāng)前頁
  :param all_count: 總頁數(shù)
  :param base_url: 模板
  :param per_page: 每頁顯示數(shù)據(jù)條數(shù)
  :param show_page: 顯示鏈接頁個(gè)數(shù)
  """
  #若url錯(cuò)誤,默認(rèn)顯示第一頁(錯(cuò)誤類型可能為:空頁面編號,非整數(shù)型頁面編號)
  try:
   self.current_page = int(current_page)
  except Exception as e:
   self.current_page = 1
  
  #根據(jù)數(shù)據(jù)庫信息條數(shù)得出總頁數(shù)   
  a, b = divmod(all_count, per_page)
  if b:
   a += 1
  self.all_page = a 
  
  self.base_url = base_url
  self.per_page = per_page
  self.show_page = show_page

 #當(dāng)前頁起始數(shù)據(jù)id
 def start_data(self):  
  return (self.current_page - 1) * self.per_page

 #當(dāng)前頁結(jié)束數(shù)據(jù)id
 def end_data(self):  
  return self.current_page * self.per_page
 
 #動態(tài)生成前端html
 def pager(self):
  page_list = []
  half = int((self.show_page - 1)/2)
  #如果:總頁數(shù) < show_page,默認(rèn)顯示頁數(shù)范圍為: 1~總頁數(shù)
  if self.all_page < self.show_page:
   start_page = 1
   end_page = self.all_page + 1
  #如果:總頁數(shù) > show_page
  else:
   #如果:current_page - half <= 0,默認(rèn)顯示頁數(shù)范圍為:1~show_page
   if self.current_page <= half:
    start_page = 1
    end_page = self.show_page + 1
   else:
    #如果:current_page + half >總頁數(shù),默認(rèn)顯示頁數(shù)范圍為:總頁數(shù) - show_page ~ 總頁數(shù)
    if self.current_page + half > self.all_page:
     end_page = self.all_page + 1
     start_page = end_page - self.show_page
    else:
     start_page = self.current_page - half
     end_page = self.current_page + half + 1

  #首頁
  first_page = "<li><a href='%s?page=%s'>首頁</a></li>" %(self.base_url, 1)
  page_list.append(first_page)

  #上一頁(若當(dāng)前頁等于第一頁,則上一頁無鏈接,否則鏈接為當(dāng)前頁減1)
  if self.current_page <= 1:
   prev_page = "<li><a href='#'>上一頁</a></li>"
  else:
   prev_page = "<li><a href='%s?page=%s'>上一頁</a></li>" %(self.base_url, self.current_page-1)
  page_list.append(prev_page)

  #動態(tài)生成中間頁數(shù)鏈接
  for i in range(start_page, end_page):
   if i == self.current_page:
    temp = "<li class='active'><a href='%s?page=%s'>%s</a></li>" %(self.base_url, i, i)
   else:
    temp = "<li><a href='%s?page=%s'>%s</a></li>" % (self.base_url, i, i)
   page_list.append(temp)

  #下一頁(若當(dāng)前頁等于最后頁,則下一頁無鏈接,否則鏈接為當(dāng)前頁加1)
  if self.current_page >= self.all_page:
   next_page = "<li><a href='#'>下一頁</a></li>"
  else:
   next_page = "<li><a href='%s?page=%s'>下一頁</a></li>" %(self.base_url, self.current_page+1)
  page_list.append(next_page)

  #末頁(若總頁數(shù)只有一頁,則無末頁標(biāo)簽)
  if self.all_page > 1:
   last_page = "<li><a href='%s?page=%s'>末頁</a></li>" % (self.base_url, self.all_page)
   page_list.append(last_page)

  return ''.join(page_list)

然后,在views中寫方法(此處寫在app01中):

from utils.pagnition import PageInfo # 從文件中導(dǎo)入上步自定義的分頁模塊

def custom(request):
 all_count = models.UserInfo.objects.all().count() 
 # 獲取要顯示數(shù)據(jù)庫的總數(shù)據(jù)條數(shù)
 page_info = PageInfo(request.GET.get('page'), all_count, '/custom.html/',)  
 # 生成分頁對象
 user_list = models.UserInfo.objects.all()[page_info.start_data():page_info.end_data()]  
 # 利用分頁對象獲取當(dāng)前頁顯示數(shù)據(jù)
 return render(request, 'custom.html', {'user_list': user_list, 'page_info': page_info}) 
 # 模板渲染

然后,在templates目錄下寫“custom.html"文件:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>customers</title>
{# 引入bootstrap樣式#}
 <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css">
</head>
<body>
<h1>customers</h1>
{#當(dāng)前頁顯示的數(shù)據(jù)#}
<ul>
 {% for row in user_list %}
  <li>{{ row.name }}</li>
 {% endfor %}
</ul>

{#分頁#}
 <nav aria-label="Page navigation">
  <ul class="pagination">
{#    傳入page_info.pager#}
   {{ page_info.pager|safe }}
  </ul>
 </nav>

</body>
</html>

最后,新增url關(guān)系(urls.py):

 from django.conf.urls import url
 from django.contrib import admin
 from app01 import views as app01_views
 
 urlpatterns = [
  url(r'^custom.html/$', app01_views.custom),
 ]

至此,就完成了利用django的分頁功能自定義分頁模塊,可以應(yīng)用在不同的業(yè)務(wù)頁面上。

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

相關(guān)文章

最新評論