django之分頁顯示

演示環境接上篇django。

說明:

Django提供了一些類,來實現管理數據分頁:Paginator對象、Page對象。

其中Paginator類對象介紹如下:

    Paginator(列表,int):返回分頁對象,參數爲列表數據,每面數據的條數;

    Paginator類對象屬性有:count:對象總數;num_pages:頁面總數;page_range:頁碼列表,從1開始,例如[1, 2, 3, 4];

    Paginator類對象方法有:page(num):下標以1開始,如果提供的頁碼不存在,拋出InvalidPage異常。


Page類對象介紹如下:

    Paginator對象的page()方法返回Page對象,不需要手動構造;

    Page類對象的屬性有:object_list:當前頁上所有對象的列表;number:當前頁的序號,從1開始;paginator:當前page對象相關的Paginator對象;

    Page類對象的方法有:has_next():如果有下一頁返回True;has_previous():如果有上一頁返回True;has_other_pages():如果有上一頁或下一頁返回True;next_page_number():返回下一頁的頁碼,如果下一頁不存在,拋出InvalidPage異常;previous_page_number():返回上一頁的頁碼,如果上一頁不存在,拋出InvalidPage異常;len():返回當前頁面對象的個數;迭代頁面對象:訪問當前頁面中的每個對象。


製作實驗所需數據,添加mysql數據:

> use test3;
> desc bookshop_heroinfo;
+----------+---------------+------+-----+---------+----------------+
| Field    | Type          | Null | Key | Default | Extra          |
+----------+---------------+------+-----+---------+----------------+
| id       | int(11)       | NO   | PRI | NULL    | auto_increment |
| hname    | varchar(10)   | NO   |     | NULL    |                |
| hgender  | tinyint(1)    | NO   |     | NULL    |                |
| hcontent | varchar(1000) | NO   |     | NULL    |                |
| isDelete | tinyint(1)    | NO   |     | NULL    |                |
| book_id  | int(11)       | NO   | MUL | NULL    |                |
+----------+---------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)

> insert into bookshop_heroinfo(hname,hgender,book_id,hcontent,isDelete) values('郭靖',1,1,'降龍十八掌',0),('黃蓉',0,1,'打狗棍法',0),('段譽',1,1,'六脈神劍',0),('虛竹',0,1,'天山六陽掌',0),('王語嫣',1,1,'神仙姐姐',0),('令狐沖',0,1,'孤獨九劍',0),('任盈盈',1,1,'彈琴',0),('嶽不羣',0,1,'華山劍法',0),('東方不敗',1,1,'葵花寶典',0),('胡斐',0,1,'胡家刀法',0),('苗若蘭',1,1,'黃衣',0),('程靈素',0,1,'醫術',0),('袁紫衣',1,1,'六合拳',0);
> select * from bookshop_heroinfo;
+----+--------------+---------+-----------------+----------+---------+
| id | hname        | hgender | hcontent        | isDelete | book_id |
+----+--------------+---------+-----------------+----------+---------+
|  1 | 郭靖         |       1 | 降龍十八掌      |        0 |       1 |
|  2 | 黃蓉         |       0 | 打狗棍法        |        0 |       1 |
|  3 | 段譽         |       1 | 六脈神劍        |        0 |       1 |
|  4 | 虛竹         |       0 | 天山六陽掌      |        0 |       1 |
|  5 | 王語嫣       |       1 | 神仙姐姐        |        0 |       1 |
|  6 | 令狐沖       |       0 | 孤獨九劍        |        0 |       1 |
|  7 | 任盈盈       |       1 | 彈琴            |        0 |       1 |
|  8 | 嶽不羣       |       0 | 華山劍法        |        0 |       1 |
|  9 | 東方不敗     |       1 | 葵花寶典        |        0 |       1 |
| 10 | 胡斐         |       0 | 胡家刀法        |        0 |       1 |
| 11 | 苗若蘭       |       1 | 黃衣            |        0 |       1 |
| 12 | 程靈素       |       0 | 醫術            |        0 |       1 |
| 13 | 袁紫衣       |       1 | 六合拳          |        0 |       1 |
+----+--------------+---------+-----------------+----------+---------+
13 rows in set (0.00 sec)

設計以5條數據爲一頁,根據上述查詢結果有13條數據,應該出現分爲3頁,每頁顯示5條記錄且最後一頁僅有3條記錄;

]# cd py3/django-test1/test5

修改視圖函數:

]# vim bookshop/views.py 
import os
from django.shortcuts import render
from django.http import HttpResponse
from django.conf import settings
from .models import *
from django.core.paginator import *

def herolist(request,pindex):
    if pindex == '':
        pindex = '1'
    list1 = HeroInfo.objects.all()
    paginator = Paginator(list1,5)
    page = paginator.page(int(pindex))
    context = {'page':page}
    return render(request, 'bookshop/herolist.html',context)
def index(request):
    return render(request,'bookshop/index.html')
def myExp(request):
    a1 = int('abc')
    return HttpResponse('hello_world')
def uploadPhoto(request):
    return render(request,'bookshop/uploadphoto.html')
def uploadHandle(request):
    pic1 = request.FILES['photo1']
    picName = os.path.join(settings.MEDIA_ROOT,pic1.name)
    with open(picName,'wb') as f:
        for p in pic1.chunks():
            f.write(p)
    return HttpResponse('<img src="/static/upload/%s" />' % pic1.name)

添加html模板:

]# vim templates/bookshop/herolist.html
<!DOCTYPE html>
<html>
<head>
    <title>Title</title>
</head>
<body>
<ul>
    {% for hero in page %}
        <li>{{ hero.hname }}</li>
    {% endfor %}
</ul>
<hr>
{% for index in page.paginator.page_range %}
    {% if index == page.number %} <!--等號兩邊必須要有空格-->

   {{ index }}<!--顯示當前頁,數字鏈接不顯示下劃線-->
    {% else %}
    <a href="/herolist/{{ index }}">{{ index }}</a>

    {% endif %}

{% endfor %}
</body>
</html>

修改url:

]# vim bookshop/urls.py 
from django.conf.urls import url
from .  import views
urlpatterns = [
    url(r'^$',views.index),
    url(r'^myexp$',views.myExp),
    url(r'^uploadphoto$',views.uploadPhoto),
    url(r'^uploadHandle$',views.uploadHandle),
    url(r'^herolist/(\d+)/$',views.herolist),
]

運行django服務器:

]# python manage.py runserver 192.168.255.70:8000

瀏覽器訪問:http://192.168.255.70:8000/herolist/1/

注意:此代碼要求第一頁時,訪問必須輸入頁號1,才能顯示,否則報錯。

頁面顯示:

QQ截圖20190212005419.png

點擊第2頁,顯示:

QQ截圖20190212005442.png

點擊第3頁,顯示:

QQ截圖20190212005449.png

且顯示的當前頁沒有鏈接的下劃線。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章