路飞学城Python-Day108

96-分页器1
批量插入的方式就不能用ORM的create()方式去做了,因为create就是对sql进行insert的操作,sql最好不要每次有一条数据就去进行插入,最好的方式就是插入一组数据
from django.shortcuts import render
from django.core.paginator import Paginator, EmptyPage
# Create your views here.
from app01.models import Book


def index(request):
    '''

    :param request:
    :return:
    '''
    # book_list = []
    # for i in range(100):
    #     book_obj = Book(title="book_%s" % i, price=i*i)
    #     book_list.append(book_obj)
    # #     进行批量插入
    # Book.objects.bulk_create(book_list)
    book_list = Book.objects.all()
    paginator = Paginator(book_list, 10)
    # print(paginator.count)
    # print(paginator.num_pages)
    try:
        c_page = int(request.GET.get('page', 1))
        c_page = paginator.page(c_page)
        # print(page1.object_list)
        for i in c_page:
            print(i.title)
    except EmptyPage as e:
        c_page = paginator.page(1)
    return render(request, 'index.html', locals())
View Code
from django.db import models

# Create your models here.


class Book(models.Model):
    title = models.CharField(max_length=32)
    price = models.DecimalField(decimal_places=2, max_digits=8)
models.py
"""PageDemo URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from app01 import views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/', views.index),
]
urls.py
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>分页器</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
    <ul>
        {% for book in c_page %}
            <li>
            {{ book.title }}:{{ book.price }}
            </li>
        {% endfor %}
    </ul>
<nav aria-label="Page navigation">
  <ul class="pagination">

        {% if c_page.has_previous%}
            <li>
            <a href="?page={{ c_page.previous_page_number }}" aria-label="Previous">
        <span aria-hidden="true">上一页</span>
        </a>
            </li>
            {% else %}
             <li class="disabled">
            <a href="" aria-label="Previous">
        <span aria-hidden="true">上一页</span>
        </a>
            </li>
        {% endif %}


        {% for foo in page_range %}
            {% if c_page_n == foo %}
                <li class="active"><a href="?page={{ foo }}">{{ foo }}</a></li>
                {% else %}
                 <li><a href="?page={{ foo }}">{{ foo }}</a></li>
            {% endif %}

        {% endfor %}

        {% if c_page.has_next %}
             <li>
            <a href="?page={{ c_page.next_page_number }}" aria-label="Next">
        <span aria-hidden="true">下一页</span>
        </a>
             </li>
            {% else %}
            <li class="disabled">
             <a href="" aria-label="Next">
        <span aria-hidden="true">下一页</span>
        </a>
             </li>
        {% endif %}


  </ul>
</nav>

</body>
</html>
index.html
97-分页器2
98-分页器3
99-分页器4
Win a contest, win a challenge
原文地址:https://www.cnblogs.com/pandaboy1123/p/9772010.html