zoukankan      html  css  js  c++  java
  • Django-分页器

    # Create your views here.
    # 导入render和HttpResponse模块
    from django.shortcuts import render, HttpResponse
    
    # 导入Paginator,EmptyPage和PageNotAnInteger模块
    from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
    
    # 从Django项目的应用中导入模块
    from app01.models import *
    
    from app01 import models
    
    def create(request):
        booklist = []
        for i in range(100):
            booklist.append(models.Book(title="book" + str(i), price=20 + i * i))
    
        models.Book.objects.bulk_create(booklist)
        return HttpResponse("ok")
    
    def index(request):
        # 获取Book数据表中的所有记录
        book_list = Book.objects.all()
    
        # 生成paginator对象,定义每页显示10条记录
        paginator = Paginator(book_list, 10)
    
        # 从前端获取当前的页码数,默认为1
        page = request.GET.get('page', 1)
    
        # 把当前的页码数转换成整数类型
        currentPage = int(page)
    
        try:
            print(page)
            book_list = paginator.page(page)  # 获取当前页码的记录
        except PageNotAnInteger:
            book_list = paginator.page(1)  # 如果用户输入的页码不是整数时,显示第1页的内容
        except EmptyPage:
            book_list = paginator.page(paginator.num_pages)  # 如果用户输入的页数不在系统的页码列表中时,显示最后一页的内容
    
        return render(request, "index.html", locals())
    views
    from django.conf.urls import url
    from django.contrib import admin
    from app01 import  views
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^create/', views.create),
        url(r'^index/', views.index),
    urls
    {% load staticfiles %}
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
              integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    </head>
    <body>
    <div class="container ">
        <div class="table-responsive">
    
            <table class="table table-hover">
                <thead>
                <tr>
                    <th>名称</th>
                    <th>价格</th>
                </tr>
                </thead>
                <tbody>
                {% for book in book_list %}
                    <tr>
                        <td >{{ book.title }} </td>
                        <td>{{ book.price }}</td>
                    </tr>
                {% endfor %}
                </tbody>
    
    
            </table>
        </div>
    
        <ul class="pagination " id="pager ">
            {#上一页按钮开始#}
            {# 如果当前页有上一页#}
            {% if book_list.has_previous %}
                {#  当前页的上一页按钮正常使用#}
                <li class="previous"><a href="./?page={{ book_list.previous_page_number }}">上一页</a></li>
            {% else %}
                {# 当前页的不存在上一页时,上一页的按钮不可用#}
                <li class="previous disabled"><a href="#">上一页</a></li>
            {% endif %}
            {#上一页按钮结束#}
            {# 页码开始#}
            {% for num in paginator.page_range %}
    
                {% if num == currentPage %}
                    <li class="item active"><a href="./?page={{ num }}">{{ num }}</a></li>
                {% else %}
                    <li class="item"><a href="./?page={{ num }}">{{ num }}</a></li>
    
                {% endif %}
            {% endfor %}
            {#页码结束#}
            {# 下一页按钮开始#}
            {% if book_list.has_next %}
                <li class="next"><a href="./?page={{ book_list.next_page_number }}">下一页</a></li>
            {% else %}
                <li class="next disabled"><a href="#">下一页</a></li>
            {% endif %}
            {# 下一页按钮结束#}
        </ul>
    </div>
    </body>
    </html>
    index
    from django.db import models
    import django.utils.timezone as timezone
    # Create your models here.
    
    
    class Book(models.Model):
        title = models.CharField(max_length=32)
        price = models.DecimalField(max_digits=6, decimal_places=2)
        create_time = models.DateField(default=timezone.now())
        memo = models.CharField(max_length=32, default="")
    models

    效果图:

     批量插入数据

     # booklist = []
        # for i in range(100):
        #     booklist.append(models.Book(name="book" + str(i), price=20 + i * i))
        #
        # models.Book.objects.bulk_create(booklist)

    当有很多页时

    # Create your views here.
    # 导入render和HttpResponse模块
    from django.shortcuts import render, HttpResponse
    
    # 导入Paginator,EmptyPage和PageNotAnInteger模块
    from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
    
    # 从Django项目的应用中导入模块
    from app01.models import *
    
    from app01 import models
    
    def create(request):
        booklist = []
        for i in range(100):
            booklist.append(models.Book(title="books" + str(i), price=20 + i * i))
        models.Book.objects.bulk_create(booklist)
    
        return HttpResponse("ok")
    
    def index(request):
        # 获取Book数据表中的所有记录
        book_list = Book.objects.all()
    
        # 生成paginator对象,定义每页显示10条记录
        paginator = Paginator(book_list, 10)
    
        # 从前端获取当前的页码数,默认为1
        page = request.GET.get('page', 1)
    
        # 把当前的页码数转换成整数类型
        currentPage = int(page)
    
        if paginator.num_pages > 30:
    
            if currentPage - 5 < 1:
                pageRange = range(1, 11)
            elif currentPage + 5 > paginator.num_pages:
                pageRange = range(currentPage - 5, paginator.num_pages + 1)
    
            else:
                pageRange = range(currentPage - 5, currentPage + 5)
    
        else:
            pageRange = paginator.page_range
        try:
            print(page)
            book_list = paginator.page(page)  # 获取当前页码的记录
        except PageNotAnInteger:
            book_list = paginator.page(1)  # 如果用户输入的页码不是整数时,显示第1页的内容
        except EmptyPage:
            book_list = paginator.page(paginator.num_pages)  # 如果用户输入的页数不在系统的页码列表中时,显示最后一页的内容
    
        return render(request, "index.html", locals())
    views
    {% load staticfiles %}
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
              integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    </head>
    <body>
    <div class="container ">
        <div class="table-responsive">
    
            <table class="table table-hover">
                <thead>
                <tr>
                    <th>名称</th>
                    <th>价格</th>
                </tr>
                </thead>
                <tbody>
                {% for book in book_list %}
                    <tr>
                        <td >{{ book.title }} </td>
                        <td>{{ book.price }}</td>
                    </tr>
                {% endfor %}
                </tbody>
    
    
            </table>
        </div>
    
        <ul class="pagination " id="pager ">
            {#上一页按钮开始#}
            {# 如果当前页有上一页#}
            {% if book_list.has_previous %}
                {#  当前页的上一页按钮正常使用#}
                <li class="previous"><a href="./?page={{ book_list.previous_page_number }}">上一页</a></li>
            {% else %}
                {# 当前页的不存在上一页时,上一页的按钮不可用#}
                <li class="previous disabled"><a href="#">上一页</a></li>
            {% endif %}
            {#上一页按钮结束#}
            {# 页码开始#}
            {% for num in pageRange %}
    
                {% if num == currentPage %}
                    <li class="item active"><a href="./?page={{ num }}">{{ num }}</a></li>
                {% else %}
                    <li class="item"><a href="./?page={{ num }}">{{ num }}</a></li>
    
                {% endif %}
            {% endfor %}
            {#页码结束#}
            {# 下一页按钮开始#}
            {% if book_list.has_next %}
                <li class="next"><a href="./?page={{ book_list.next_page_number }}">下一页</a></li>
            {% else %}
                <li class="next disabled"><a href="#">下一页</a></li>
            {% endif %}
            {# 下一页按钮结束#}
        </ul>
    </div>
    </body>
    </html>
    index

    效果图

  • 相关阅读:
    启动mysql时显示:/tmp/mysql.sock 不存在的解决方法
    python在使用MySQLdb模块时报Can't extract file(s) to egg cacheThe following error occurred while trying to extract file(s) to the Python eggcache的错误。
    文档发布测试
    我的日常
    CSS-基础优化策略
    Git-基本操作
    Git-配置SSH公钥
    Git-免密提交
    Wx-mpvue开发小程序
    Vue-移动端开发全家桶
  • 原文地址:https://www.cnblogs.com/yinxin/p/9055683.html
Copyright © 2011-2022 走看看