Django前端BootCSS實現(xiàn)分頁的方法
通過使用bootstrap框架,并配合Django自帶的Paginator分頁組件即可實現(xiàn)簡單的分頁效果。
1.創(chuàng)建MyWeb項目
python manage.py startapp MyWeb
2.修改settings.py配置文件,導(dǎo)入我們的app的名字,去掉csrf這個選項
# 屏蔽一項
MIDDLEWARE = [
#'django.middleware.csrf.CsrfViewMiddleware'
]
# 新增一項
TEMPLATES = [
'MyWeb.apps.MywebConfig'
]
3.來urls.py里面寫一條路由,名字就叫index/映射到views.index函數(shù)下處理此請求
from MyWeb import views
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.index)
]
4.最后在myweb里面的views.py設(shè)置一個視圖函數(shù),最后運行
from django.shortcuts import render
from django.shortcuts import HttpResponse
from MyWeb import models
def index(requests):
return HttpResponse("abcd")
5.配置數(shù)據(jù)庫文件models.py并設(shè)置以下內(nèi)容
from django.db import models
# 創(chuàng)建用戶表
class User(models.Model):
id = models.AutoField(primary_key=True)
username = models.CharField(max_length=32)
password = models.CharField(max_length=32)
6.更新數(shù)據(jù)庫與數(shù)據(jù)表
python manage.py makemigrations # 將你的數(shù)據(jù)庫變動記錄下來(并不會幫你創(chuàng)建表) python manage.py migrate # 將你的數(shù)據(jù)庫變動正在同步到數(shù)據(jù)庫中
7.增加一個新的view并使用rand()函數(shù).
首先在urls.py中增加路由
from django.contrib import admin
from django.urls import path
from MyWeb import views
urlpatterns = [
path('admin/', admin.site.urls),
path('index/',views.index),
path('rand/',views.rand)
]
其次在view.py視圖中增加生成函數(shù).
from django.shortcuts import render
from django.shortcuts import HttpResponse
from MyWeb import models
import random
# 首頁
def index(requests):
return HttpResponse("abcd")
# 生成測試數(shù)據(jù)
def rand(request):
for i in range(1,1000):
chars = []
pasd = []
for x in range(1,8):
chars.append(random.choice('abcdefghijklmnopqrstuvwxyz'))
pasd.append(random.choice('0987654321'))
user = "".join(chars)
pwd = "".join(pasd)
models.User.objects.create(username=user, password=pwd)
return HttpResponse("ok")
啟動django并訪問http://127.0.0.1:8000/rand/等待數(shù)據(jù)生成結(jié)束.
8.在templates模板中,新增一個page.html頁面。
<!--name: page.html-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" rel="external nofollow" >
</head>
<body>
<table class="table table-sm table-hover">
<thead>
<tr class="table-success">
<th> 序號</th> <th> 用戶名</th> <th> 用戶密碼</th>
</tr>
</thead>
<tbody>
{% for article in user_list %}
<tr class="table-primary">
<td>{{ article.id }}</td>
<td>{{ article.username }}</td>
<td>{{ article.password }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<nav class="d-flex justify-content-center" aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item"><a class="page-link" href="./page?id=1" rel="external nofollow" >首頁</a></li>
{% if user_list.has_previous %}
<li class="page-item"><a class="page-link" href="./page?id={{ user_list.previous_page_number }}" rel="external nofollow" >上一頁</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link" href="#" rel="external nofollow" rel="external nofollow" >上一頁</a></li>
{% endif %}
{% for item in page_range %}
{% if item == currentPage %}
<li class="page-item active"><a class="page-link" href="./page?id={{ item }}" rel="external nofollow" rel="external nofollow" >{{ item }}</a></li>
{% else %}
<li class="page-item"><a class="page-link" href="./page?id={{ item }}" rel="external nofollow" rel="external nofollow" >{{ item }}</a></li>
{% endif %}
{% endfor %}
{% if user_list.has_next %}
<li class="page-item"><a class="page-link" href="./page?id={{ user_list.next_page_number }}" rel="external nofollow" >下一頁</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link" href="#" rel="external nofollow" rel="external nofollow" >下一頁</a></li>
{% endif %}
<li class="page-item"><a class="page-link" href="./page?id={{ paginator.num_pages }}" rel="external nofollow" >尾頁</a></li>
</ul>
</nav>
<div style="text-align: center;" class="alert alert-dark">
統(tǒng)計: {{ currentPage }}/{{ paginator.num_pages }} 共查詢到:{{ paginator.count }} 條數(shù)據(jù) 頁碼列表:{{ paginator.page_range }}
</div>
</body>
</html>
9.最后在路由曾以及view中增加對應(yīng)的URL以及路由函數(shù).
首先在urls.py中增加一條新路由.
from django.contrib import admin
from django.urls import path
from MyWeb import views
urlpatterns = [
path('admin/', admin.site.urls),
path('index/',views.index),
path('rand/',views.rand),
path('page',views.page)
]
接著在views.py中增加一個page函數(shù).
from django.shortcuts import render
from django.shortcuts import HttpResponse
from MyWeb import models
import random
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
# 首頁
def index(requests):
return HttpResponse("abcd")
# 生成測試數(shù)據(jù)
def rand(request):
for i in range(1,1000):
chars = []
pasd = []
for x in range(1,8):
chars.append(random.choice('abcdefghijklmnopqrstuvwxyz'))
pasd.append(random.choice('0987654321'))
user = "".join(chars)
pwd = "".join(pasd)
models.User.objects.create(username=user, password=pwd)
return HttpResponse("ok")
# 分頁函數(shù)
def page(request):
user = models.User.objects.all()
paginator = Paginator(user, 10)
currentPage = int(request.GET.get("id",1))
if paginator.num_pages > 15:
if currentPage-5 < 1:
pageRange = range(1,11)
elif currentPage+5 > paginator.num_pages:
pageRange = range(currentPage-5,paginator.num_pages)
else:
pageRange = range(currentPage-5,currentPage+5)
else:
pageRange = paginator.page_range
try:
user_list = paginator.page(currentPage)
except PageNotAnInteger:
user_list = paginator.page(1)
except:
user_list = paginator.page(paginator.num_pages)
return render(request,"page.html",{"user_list":user_list,
"paginator":paginator,
"page_range":pageRange,
"currentPage":currentPage})
準(zhǔn)備就緒之后,直接訪問http://127.0.0.1:8000/page即可看到分頁顯示效果.

到此這篇關(guān)于Django前端BootCSS實現(xiàn)分頁的方法的文章就介紹到這了,更多相關(guān)Django BootCSS分頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
2024最新Pytorch安裝圖文教程(Anaconda+GPU)
本文詳細(xì)介紹了如何使用Anaconda創(chuàng)建虛擬環(huán)境,并在該環(huán)境中安裝CUDA和cuDNN,最后安裝支持GPU的PyTorch,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-11-11
Python抓新型冠狀病毒肺炎疫情數(shù)據(jù)并繪制全國疫情分布的代碼實例
在本篇文章里小編給大家整理了一篇關(guān)于Python抓新型冠狀病毒肺炎疫情數(shù)據(jù)并繪制全國疫情分布的代碼實例,有興趣的朋友們可以學(xué)習(xí)下。2020-02-02
python統(tǒng)計多維數(shù)組的行數(shù)和列數(shù)實例
今天小編就為大家分享一篇python統(tǒng)計多維數(shù)組的行數(shù)和列數(shù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
如何用python獲取到照片拍攝時的詳細(xì)位置(附源碼)
其實我們平時拍攝的照片里,隱藏了大量的信息,下面這篇文章主要給大家介紹了關(guān)于如何用python獲取到照片拍攝時的詳細(xì)位置,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
Python實現(xiàn)優(yōu)先級隊列結(jié)構(gòu)的方法詳解
優(yōu)先級隊列(priority queue)是0個或多個元素的集合,每個元素都有一個優(yōu)先權(quán),接下來就來看一下簡潔的Python實現(xiàn)優(yōu)先級隊列結(jié)構(gòu)的方法詳解:2016-06-06

