# name: models.py from django.db import models class User(models.Model): id = models.AutoField(primary_key=True) username = models.CharField(max_length=32) password = models.CharField(max_length=32) # 插入测试数据 import random def index(request): for i in range(1,100): 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")
<!--name: page.html--> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" 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" rel="external nofollow" >首页</a></li> {% i<strong style="color:transparent">来源gaodai#ma#com搞@代~码$网</strong>f user_list.has_previous %} <li class="page-item"><a class="page-link" href="./page?id={{ user_list.previous_page_number }}" rel="external nofollow" rel="external nofollow" >上一页</a></li> {% else %} <li class="page-item disabled"><a class="page-link" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上一页</a></li> {% endif %} {% for item in paginator.page_range %} {% if item == currentPage %} <li class="page-item active"><a class="page-link" href="./page?id={{ item }}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 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" rel="external nofollow" >下一页</a></li> {% else %} <li class="page-item disabled"><a class="page-link" href="#" rel="external nofollow" rel="external nofollow" 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" rel="external nofollow" >尾页</a></li> </ul> </nav> <div style="text-align: center;" class="alert alert-dark"> 统计: {{ currentPage }}/{{ paginator.num_pages }} 共查询到:{{ paginator.count }} 条数据 页码列表:{{ paginator.page_range }} </div> </body> </html>
# name: views.py from django.shortcuts import render,HttpResponse from MyWeb import models from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger def page(request): user = models.User.objects.all() paginator = Paginator(user, 10) currentPage = int(request.GET.get("id",1)) 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, "currentPage":currentPage})
# name: urls.py from MyWeb import views urlpatterns = [ path('admin/', admin.site.urls), path('page',views.page) ]