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

Django自定義分頁與bootstrap分頁結(jié)合

 更新時(shí)間:2021年02月22日 11:02:29   作者:云中不知人  
這篇文章主要為大家詳細(xì)介紹了Django自定義分頁與bootstrap分頁結(jié)合使用的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

django中有自帶的分頁模塊Paginator,想Paginator提供對(duì)象的列表,就可以提供每一頁上對(duì)象的方法。

這里的話不講解Paginator,而是自定義一個(gè)分頁類來完成需求:

class Pagination(object): 
 """用于Model字段值的選擇""" 
 
 def __init__(self): 
  pass 
 
 @classmethod 
 def create_pagination(self, from_name='', model_name='', 
       cur_page=1, start_page_omit_symbol = '...', 
       end_page_omit_symbol = '...', one_page_data_size=10, 
       show_page_item_len=9): 
  """通過給的model和分頁參數(shù)對(duì)相關(guān)model進(jìn)行分頁 
  Args: 
   from_name: 導(dǎo)入模塊的 from后面的參數(shù) 
    from {from_name} import model_name 
   mode_name: 需要導(dǎo)入的模塊名 
    from from_name import {model_name} 
   cur_page: 當(dāng)前顯示的是第幾頁 
   start_page_omit_symbol: 超出的頁數(shù)使用怎么樣的省略號(hào)(前) 
    ... 2 3 4 
   end_page_omit_symbol: 超出的頁數(shù)使用怎么樣的省略號(hào)(后) 
    1 2 3 4 ... 
   one_page_data_size: 每一頁顯示幾行 
   show_page_item_len: 顯示幾個(gè)能點(diǎn)擊的頁數(shù) 
  Return: 
   pagination: dict 
     pagination = { 
      'objs': objs, # 需要顯示model數(shù)據(jù) 
      'all_obj_counts': all_obj_counts, # 一共多少行數(shù)據(jù) 
      'start_pos': start_pos, # 數(shù)據(jù)分頁開始的數(shù)據(jù) 
      'end_pos': end_pos, # 數(shù)據(jù)分頁結(jié)束的數(shù)據(jù) 
      'all_page': all_page, # 一共有多少頁 
      'cur_page': cur_page, # 當(dāng)前的頁碼 
      'pre_page': pre_page, # 上一頁的頁碼 
      'next_page': next_page, # 下一頁的頁碼 
      'page_items': page_items, 能點(diǎn)擊的頁數(shù) 
      'start_page_omit_symbol': start_page_omit_symbol, # 開始的省略號(hào) 
      'end_page_omit_symbol': end_page_omit_symbol, # 結(jié)束的省略號(hào) 
     } 
  Raise: None 
  """ 
  # 如果沒有輸入導(dǎo)入模塊需要的相關(guān)信息直接退出 
  if not from_name or not model_name: 
   return None 
 
  import_str = 'from {from_name} import {model_name}'.format( 
           from_name = from_name, 
           model_name = model_name) 
  # 導(dǎo)入模塊 
  exec import_str 
 
  start_pos = (cur_page - 1) * one_page_data_size 
  end_pos = start_pos + one_page_data_size 
 
  # 查找需要的model數(shù)據(jù) 
  find_objs_str = ('{model_name}.objects.all()' 
       '[{start_pos}:{end_pos}]'.format( 
            model_name = model_name, 
            start_pos = start_pos, 
            end_pos = end_pos)) 
  objs = eval(find_objs_str) 
  
  # 計(jì)算總共的頁數(shù) 
  find_objs_count_str = '{model_name}.objects.count()'.format( 
            model_name = model_name) 
  all_obj_counts = eval(find_objs_count_str) 
  all_page = all_obj_counts / one_page_data_size 
  remain_obj = all_obj_counts % one_page_data_size 
  if remain_obj > 0: 
   all_page += 1 
 
  # 限制當(dāng)前頁不能小于1和并且大于總頁數(shù) 
  cur_page = 1 if cur_page < 1 else cur_page 
  cur_page = all_page if cur_page > all_page else cur_page 
 
  # 獲得顯示頁數(shù)的最小頁 
  start_page = cur_page - show_page_item_len / 2 
  if start_page > all_page - show_page_item_len: 
   start_page = all_page - show_page_item_len + 1 
  start_page = 1 if start_page < 1 else start_page 
 
  # 獲得顯示頁數(shù)的最大頁 
  end_page = cur_page + show_page_item_len / 2 
  end_page = all_page if end_page > all_page else end_page 
  if end_page < show_page_item_len and all_page > show_page_item_len: 
   end_page = show_page_item_len 
 
  # 獲得上一頁 
  pre_page = cur_page - 1 
  pre_page = 1 if pre_page < 1 else pre_page 
 
  # 獲得下一頁 
  next_page = cur_page + 1 
  next_page = all_page if next_page > all_page else next_page 
 
  # 處理省略符,是否顯示 
  if start_page <= 1: 
   start_page_omit_symbol = '' 
   
  if end_page >= all_page: 
   end_page_omit_symbol = '' 
 
  # 創(chuàng)建能點(diǎn)擊的展示頁碼 
  page_items = range(start_page, end_page + 1) 
 
  pagination = { 
   'objs': objs, 
   'all_obj_counts': all_obj_counts, 
   'start_pos': start_pos, 
   'end_pos': end_pos, 
   'all_page': all_page, 
   'cur_page': cur_page, 
   'pre_page': pre_page, 
   'next_page': next_page, 
   'page_items': page_items, 
   'start_page_omit_symbol': start_page_omit_symbol, 
   'end_page_omit_symbol': end_page_omit_symbol, 
  } 
 
  return pagination 

利用bootstrap的css,生成好看的html如下:

<nav aria-label="Page navigation"> 
 <ul class="pagination"> 
 {% if pagination.cur_page != 1 %} 
 <li><a href="?cur_page=1" rel="external nofollow" ><<</a></li> 
 <li> 
  <a href="?cur_page={{ pagination.pre_page }}" rel="external nofollow" aria-label="Previous"> 
  <span aria-hidden="true">«</span> 
  </a> 
 </li> 
 {% endif %} 
 {% for page_item in pagination.page_items %} 
 {% if page_item == pagination.cur_page %} 
  <li><a href="?cur_page={{ page_item }}" rel="external nofollow" rel="external nofollow" >{{ page_item }}</a></li> 
 {% else %} 
  <li><a href="?cur_page={{ page_item }}" rel="external nofollow" rel="external nofollow" >{{ page_item }}</a></li> 
 {% endif %} 
 {% endfor%} 
 {% if pagination.cur_page != pagination.all_page %} 
 <li> 
  <a href="?cur_page={{ pagination.next_page }}" rel="external nofollow" aria-label="Next"> 
  <span aria-hidden="true">»</span> 
  </a> 
 </li> 
 <li><a href="?cur_page={{ pagination.all_page }}" rel="external nofollow" >>></a></li> 
 {% endif %} 
 </ul> 
</nav> 

view函數(shù)如下:

def blogpage(request): 
  #ojt = BlogPost.object.all() 
  #p = Paginator(ojt,2) 
  #page_count = p.count 
  #page_data = p.page(page) 
  #template = 'blogpage.html' 
  #info = {'page_data':page_data,'page_count':page_count} 
  #return render(request,template,{'page_data':page_data,'page_count':page_count}) 
  try: 
    cur_page = int(request.GET.get('cur_page', '1')) 
  except ValueError: 
    cur_page = 1 
 
  pagination = Pagination.create_pagination( 
       from_name='blog.models', 
       model_name='BlogPost', 
       cur_page=cur_page, 
       start_page_omit_symbol = '...', 
       end_page_omit_symbol = '...', 
       one_page_data_size=1, 
       show_page_item_len=5) 
  return render(request, 'blogpage.html',{'pagination':pagination}) 

效果圖如下:

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

相關(guān)文章

最新評(píng)論