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/ 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):         
    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 = Paginator(subjects, 5)
    page = request.GET.get('page')
        subject_obj =
    except PageNotAnInteger:
        subject_obj =
    except (EmptyPage, InvalidPage):
        subject_obj =
    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”>
                <!—  分页功能-->
                    <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>
                        {% 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>
                            {% else %}
                                    <a href="?page={{ page_num }}">{{ page_num }}
                                        <span class="sr-only">(current)</span>
                            {% 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>
                        {% endif %}

Fourth, the final display effect


(Image from Internet)

Author: Ren Hongli

Source:Yixin Institute of Technology