Django實現(xiàn)網(wǎng)頁分頁功能
本文實例為大家分享了Django實現(xiàn)分頁功能,為了容易區(qū)別功能的展現(xiàn),先創(chuàng)建一個數(shù)據(jù)庫,用數(shù)據(jù)庫中的數(shù)據(jù)做演示。
創(chuàng)建數(shù)據(jù)庫步驟如下:
1.創(chuàng)建模型,代碼如下:
from django.db import models # Create your models here. class Goods(models.Model): name = models.CharField(max_length=100) des = models.CharField(max_length=1000) class Meta: db_table = 'goods'
2.向數(shù)據(jù)庫中添加數(shù)據(jù),代碼如下:
from django.http import HttpResponse from .models import Goods import random # Create your views here. def index(request): for x in range(200): good = Goods(name='good%s'%x,des='該商品物美價廉,現(xiàn)在只需要{}元'.format(random.randint(10,100))) good.save() return HttpResponse('數(shù)據(jù)添加成功')
添加之后將代碼就可以注釋掉了,我們演示的時候再新建一個select接口作為演示界面。
from django.shortcuts import render from django.core.paginator import Paginator , PageNotAnInteger,EmptyPage # Create your views here. def select(request): # 查詢數(shù)據(jù)庫中的所有數(shù)據(jù) good_list = Goods.objects.all() # 值1:所有的數(shù)據(jù) # 值2:每一頁的數(shù)據(jù) # 值3:當(dāng)最后一頁數(shù)據(jù)少于n條,將數(shù)據(jù)并入上一頁 paginator = Paginator(good_list,12,3) try: # GET請求方式,get()獲取指定Key值所對應(yīng)的value值 # 獲取index的值,如果沒有,則設(shè)置使用默認(rèn)值1 num = request.GET.get('index','1') # 獲取第幾頁 number = paginator.page(num) except PageNotAnInteger: # 如果輸入的頁碼數(shù)不是整數(shù),那么顯示第一頁數(shù)據(jù) number = paginator.page(1) except EmptyPage: number = paginator.page(paginator.num_pages) # 將當(dāng)前頁頁碼,以及當(dāng)前頁數(shù)據(jù)傳遞到index.html return render(request,'index.html',{'page':number,'paginator':paginator})
下面我們在index.html做界面處理,這里是需要用到bootstraps,三個文件如下圖所示,前面講解過。
來到index.html文件,做如下設(shè)置:
{% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="{% static 'css/bootstrap.css' %}" > <!--<link rel="stylesheet" href="">--> <title>淘寶</title> </head> <body> {% for good in page.object_list %} <h4>{{good.name}} - {{good.des}}</h4> {% endfor %} <ul class="pagination"> {# 如果當(dāng)前頁還有上一頁 #} {% if page.has_previous %} <li> {# 點(diǎn)擊a標(biāo)簽,跳轉(zhuǎn)到上一頁鏈接 ?index 為地址后面拼接的參數(shù) #} <a href="?index={{page.previous_page_number}}" >上一頁</a> </li> {% else %} {# 如果沒有上一頁, 則上一頁按鈕不可點(diǎn)擊 #} <li class="disabled"> <a href="#">上一頁</a> </li> {% endif %} {% for page_number in paginator.page_range %} {# 獲取當(dāng)前頁的頁碼 #} {% if page_number == page.number %} {# 如果是當(dāng)前頁的話,選中 #} <li class="active"> <a href="?index={{page_number}}">{{page_number}}</a> </li> {% else %} <li> <a href="?index={{page_number}}">{{page_number}}</a> </li> {% endif %} {% endfor %} {% if page.has_next %} <li> <a href="?index={{page.next_page_number}}">下一頁</a> </li> {% else %} <li class="disabled"> <a href="#" >下一頁</a> </li> {% endif %} </ul> <script src="{% static 'js/jquery.js' %}"></script> <script src="{% static 'js/bootstrap.js' %}"></script> </body> </html>
顯示效果如下:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python調(diào)用騰訊API實現(xiàn)人臉身份證比對功能
這篇文章主要介紹了Python調(diào)用騰訊API進(jìn)行人臉身份證比對,簡單介紹了調(diào)用騰訊云API步驟,通過完整代碼展示與結(jié)果,需要的朋友可以參考下2022-04-04Python中用函數(shù)作為返回值和實現(xiàn)閉包的教程
這篇文章主要介紹了Python中用函數(shù)作為返回值和實現(xiàn)閉包的教程,代碼基于Python2.x版本,需要的朋友可以參考下2015-04-04Python如何利用xlrd和xlwt模塊操作Excel表格
這篇文章主要給大家介紹了關(guān)于Python如何利用xlrd和xlwt模塊操作Excel表格的相關(guān)資料,其中xlrd模塊實現(xiàn)對excel文件內(nèi)容讀取,xlwt模塊實現(xiàn)對excel文件的寫入,需要的朋友可以參考下2022-03-03matplotlib常見函數(shù)之plt.rcParams、matshow的使用(坐標(biāo)軸設(shè)置)
這篇文章主要介紹了matplotlib常見函數(shù)之plt.rcParams、matshow的使用(坐標(biāo)軸設(shè)置),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01python多進(jìn)程中的內(nèi)存復(fù)制(實例講解)
下面小編就為大家分享一篇python多進(jìn)程中的內(nèi)存復(fù)制(實例講解),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01Python3利用openpyxl讀寫Excel文件的方法實例
這篇文章主要給大家介紹了關(guān)于Python3利用openpyxl讀寫Excel文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02