Django框架實(shí)現(xiàn)的分頁demo-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)Django框架實(shí)現(xiàn)的分頁demo,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、義安網(wǎng)絡(luò)推廣、成都微信小程序、義安網(wǎng)絡(luò)營銷、義安企業(yè)策劃、義安品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供義安建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:muchs.cn

首先初始化model,建表

class Book(models.Model):
  name = models.CharField(max_length=20)
  def __str__(self):
    return self.name
  class Meta:
    db_table = 'books'

然后用pycharm的數(shù)據(jù)庫模塊可視化插入

分頁思路

url傳遞參數(shù)http://127.0.0.1:8000/books/?page=5比如這樣傳遞的參數(shù)就是5,就顯示第五頁,

1.get到所有圖書對(duì)象

2.計(jì)算好每一頁應(yīng)該有幾個(gè)數(shù)據(jù)

3.根據(jù)不同的page值傳遞

def books(request):
  #取從url傳遞的參數(shù)
  page_num = request.GET.get('page')
  page_num = int(page_num)
  start = (page_num-1)*5
  end = page_num*5
  #總頁碼數(shù)是?
  per_page = 5
  total = models.Book.objects.all().count()
  total,more =divmod(total,per_page)
  if more:
    total+=1
  all_books = models.Book.objects.all()[start:end]
  #自己拼接分頁的html代碼
  html_str_list = []
  for i in range(1,total):
    tmp = '<li><a href="/books/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{}</li>'.format(i,i)
    html_str_list.append(tmp)
  page_html = "".join(html_str_list)
  return render(request,'books.html',{'books':all_books,'total_page':total,'page_html':page_html})

拿到數(shù)據(jù)總量的值,每一頁的數(shù)量為5,如果有余數(shù)則total+1也就是增加一個(gè)頁面.

建立一個(gè)列表,去拼接a標(biāo)簽,最后傳遞給前端

前端

前端的樣式用到了boottrap,可以直接看文檔.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>書記列表</title>
  <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.css" rel="external nofollow" >
</head>
<body>
<div class="container">
  <table class="table table-bordered">
    <thead>
    <tr>
      <th>序號(hào)</th>
      <th>id</th>
      <th>書名</th>
    </tr>
    </thead>
    <tbody>
    {% for book in books %}
      <tr>
        <td>{{ forloop.counter }}</td>
        <td>{{ book.id }}</td>
        <td>{{ book.name }}</td>
      </tr>
    {% endfor %}
    </tbody>
  </table>
<nav aria-label="Page navigation">
 <ul class="pagination">
  <li>
   <a href="#" rel="external nofollow" rel="external nofollow" aria-label="Previous">
    <span aria-hidden="true">&laquo;</span>
   </a>
  </li>
   {{ page_html|safe }}
  <li>
   <a href="#" rel="external nofollow" rel="external nofollow" aria-label="Next">
    <span aria-hidden="true">&raquo;</span>
   </a>
  </li>
 </ul>
</nav>
</div>
</body>
</html>
{{ page_html|safe }}

傳遞過來的page_html要用safe過濾器,不然無法轉(zhuǎn)移成html.

最終效果

Django框架實(shí)現(xiàn)的分頁demo

分頁優(yōu)化

設(shè)置一個(gè)首頁一個(gè)尾頁,以及顯示局部的頁面

def books(request):
  # 取從url傳遞的參數(shù)
  page_num = request.GET.get('page')
  page_num = int(page_num)
  start = (page_num - 1) * 5
  end = page_num * 5
  # 總頁碼數(shù)是?
  per_page = 5
  # 頁面上總共展示多少頁面
  max_page = 11
  half_max_page = max_page // 2
  # 頁面上展示的頁面從哪開始
  page_start = page_num - half_max_page
  if page_start <= 1:
    page_start = 1
  total = models.Book.objects.all().count()
  # 頁面到哪結(jié)束
  page_end = page_num+half_max_page
  if page_end > total:
    page_end = total
    page_start = total - max_page
  total, more = divmod(total, per_page)
  if more:
    total += 1
  all_books = models.Book.objects.all()[start:end]
  # 自己拼接分頁的html代碼
  html_str_list = []
  html_str_list.append('<li><a href="/books/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</li>'.format(1,1))
  for i in range(page_start, page_end+1):
    tmp = '<li><a href="/books/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{}</li>'.format(i, i)
    html_str_list.append(tmp)
  html_str_list.append('<li><a href="/books/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >最后一頁</li>'.format(total))
  page_html = "".join(html_str_list)
  return render(request, 'books.html', {'books': all_books, 'total_page': total, 'page_html': page_html})

關(guān)于Django框架實(shí)現(xiàn)的分頁demo就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

新聞標(biāo)題:Django框架實(shí)現(xiàn)的分頁demo-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://muchs.cn/article48/degghp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)站內(nèi)鏈、企業(yè)建站、響應(yīng)式網(wǎng)站、自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)