Three Steps to Realize Django Paginator Pagination

  django, Paging

Django provides a new class to help manage paging data. This class is stored in Django/Core/Paginator.py. It can receive lists, tuples, or other iterated objects. This article will introduce the implementation steps of Django Paginator paging in three steps.

First, create a Subject through the model

Django models

from django.db import models 
class Subject(models.Model):     
    """学科""”     
    no = models.AutoField(primary_key=True, verbose_name="编号")     
    name = models.CharField(max_length=31, verbose_name="名称")     
    intro = models.CharField(max_length=511, verbose_name="介绍")     
    def __str__(self):         
        return self.name     
    
    class Meta:         
        db_table = 'tb_subject’         
        verbose_name_plural = "学科"

Second, through the view module, data to be presented to the front end

Configuration of Django view

from django.shortcuts import render
from django.core.paginator import Paginator, InvalidPage, EmptyPage, PageNotAnInteger
from vote.models import Subject
def show_subject(request):
    """查询学科"""
    #查询Subject 表的所有数据
    subjects = Subject.objects.all().order_by("no”)
    #使用Paginator模块对数据分页,一页5条数据
    paginator = Paginator(subjects, 5)
    #使用request.GET.get()函数获取uri中的page参数的数值
    page = request.GET.get('page')
    try:
        #通过获取上面的page参数,查询此page是否为整数并且是否可用
        subject_obj = paginator.page(page)
    except PageNotAnInteger:
        subject_obj = paginator.page(1)
    except (EmptyPage, InvalidPage):
        subject_obj = paginator.page(paginator.num_pages)
    return render(request, "vote/subject.html", {'subject_list': subject_obj})

3. Rendering the data in view to the front-end template

Front-end paging code block:

<div class="pull-right" style="margin-right: 480px;margin-top: 0px”>
                <!—  分页功能-->
                <nav>
                    <ul class="pagination”>
                        <!—  分页功能前端的方向箭头代码-->
                        {% if subject_list.has_previous %}
                        <li class="enabled">
                            <a href="?page={{ subject_list.previous_page_number }}" aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                        {% endif %}
                        <!—  分页功能显示页码的中间部分代码-->
                        {% for page_num in subject_list.paginator.page_range %}
                            {% if page_num == subject_list.number %}
                                <li class="active">
                                    <a href="#">
                                        {{ page_num }}
                                        <span class="sr-only">(current)</span>
                                    </a>
                                </li>
                            {% else %}
                                <li>
                                    <a href="?page={{ page_num }}">{{ page_num }}
                                        <span class="sr-only">(current)</span>
                                    </a>
                                </li>
                            {% endif %}
                        {% endfor %}
                        <!—  分页功能末端的方向箭头代码-->
                        {% if  subject_list.has_next %}
                        <li class="enabled">
                            <a href="?page={{ subject_list.next_page_number }}" aria-label="last">
                                <span aria-hidden="true">&raquo;</span>
                            </a>
                        </li>
                        {% endif %}
                    </ul>
                </nav>
</div>

Fourth, the final display effect

图片描述

(Image from Internet)

Author: Ren Hongli

Source:Yixin Institute of Technology