django的分頁器Paginator 從django中導(dǎo)入類
先創(chuàng)建表,然后生成批量數(shù)據(jù)。
在models文件里
from django.db import models # Create your models here. class Book(models.Model): name = models.CharField(max_length=32) price = models.DecimalField(max_digits=5,decimal_places=2)
然后執(zhí)行python manage.py makemigrations ,python migrate 生成數(shù)據(jù)庫。把數(shù)據(jù)庫從左邊拉到右邊,
在url里創(chuàng)建showBooks視圖函數(shù)API,
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^index/', views.index), url(r'^ajaxHandle/', views.ajaxHandle), url(r'^showBooks/', views.showBooks),-------
在views文件中創(chuàng)建showBooks 函數(shù),批量導(dǎo)入數(shù)據(jù),用bulk_create()
從django中導(dǎo)入Paginstor類,用對(duì)象調(diào)用方法,
def showBooks(requests): #批量導(dǎo)入數(shù)據(jù)bulk_create()方法 # book_list=[]#里面存一個(gè)個(gè)對(duì)象 # for i in range(100): # book_list.append(Book(name="book%s"%i,price=2+i+2)) # # Book.objects.bulk_create(book_list) book_list_all = Book.objects.all() #分頁器Paginator,是導(dǎo)入了一個(gè)類,在用實(shí)列出來的對(duì)象調(diào)用方法, from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger #book_list_all 是要被分頁的對(duì)象,第二個(gè)參數(shù),是每頁顯示的條數(shù) p = Paginator(book_list_all,20)# p就是每頁的對(duì)象, p.count #數(shù)據(jù)總數(shù) p.num_pages #總頁數(shù) p.page_range#[1,2,3,4,5],得到頁碼,動(dòng)態(tài)生成, page_num = requests.GET.get("page")#以get的方法從url地址中獲取 #如果輸錯(cuò)了頁碼, try: book_list = p.page(page_num)#括號(hào)里的是頁數(shù),顯示指定頁碼的數(shù)據(jù),動(dòng)態(tài)顯示數(shù)據(jù),所以不能寫死了 except PageNotAnInteger:#如果輸入頁碼錯(cuò)誤,就顯示第一頁 book_list = p.page(1) except EmptyPage:#如果超過了頁碼范圍,就把最后的頁碼顯示出來, book_list = p.page(p.num_pages) return render(requests,"showBooks.html",locals())
數(shù)據(jù)庫生成數(shù)據(jù)
在templates 創(chuàng)建showBooks頁面,把數(shù)據(jù)庫數(shù)據(jù)渲染出來
{% load staticfiles %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="{% static 'bootstrap/css/bootstrap.css' %}" rel="external nofollow" > </head> <body> <ul> {#request 也可以在這里渲染出來#} {% for book in book_list %} <li>{{ book.id }}     {{ book.name }}     {{ book.price }}</li> {% endfor %} </ul> <ul class="pagination"> {% if book_list.has_previous %} <li><a href="/showBooks/?page={{ book_list.previous_page_number }}" rel="external nofollow" >上一頁</a></li> ---直接使用方法,上一頁, {% else %} <li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" >上一頁</a></li> {% endif %} {% for num in p.page_range %} <li><a href="/showBooks/?page={{ num }}" rel="external nofollow" >{{ num }}</a></li> {% endfor %} {% if book_list.has_next %} <li><a href="/showBooks/?page={{ book_list.next_page_number }}" rel="external nofollow" >下一頁</a></li> {% else %} <li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" >下一頁</a></li> {% endif %} </ul> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python __getattr__與__setattr__使用方法
__getattr__和__setattr__可以用來對(duì)屬性的設(shè)置和取值進(jìn)行處理2008-09-09Python IndexError報(bào)錯(cuò)分析及解決方法
在Python編程中,IndexError是一種常見的異常類型,它通常發(fā)生在嘗試訪問序列(如列表、元組或字符串)中不存在的索引時(shí),本文將深入分析IndexError的成因、表現(xiàn)形式,并提供相應(yīng)的解決辦法,同時(shí)附帶詳細(xì)的代碼示例,需要的朋友可以參考下2024-07-07Python數(shù)據(jù)分析之Python和Selenium爬取BOSS直聘崗位
今天教各位小伙伴怎么用Python和Selenium爬取BOSS直聘崗位,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python爬蟲和數(shù)據(jù)分析的小伙伴有很好地幫助,需要的朋友可以參考下2021-05-05如何利用Python和matplotlib更改縱橫坐標(biāo)刻度顏色
對(duì)于圖表來說最簡(jiǎn)單的莫過于作出一個(gè)單一函數(shù)的圖像,下面這篇文章主要給大家介紹了關(guān)于如何利用Python和matplotlib更改縱橫坐標(biāo)刻度顏色的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08Python讀取ini文件、操作mysql、發(fā)送郵件實(shí)例
這篇文章主要介紹了Python讀取ini文件、操作mysql、發(fā)送郵件實(shí)例,本文重點(diǎn)在Mysql操作的講解上,包含查詢、插入、更新和刪除操作,需要的朋友可以參考下2015-01-01