三步實現Django Paginator 分頁

Django提供了一個新的類來幫助管理分頁數據,這個類存放在django/core/paginator.py.它能夠接收列表、元組或其它可迭代的對象。本文將分三步介紹Django Paginator 分頁的實現步驟html

1、經過模型建立Subject

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 = "學科" 複製代碼

2、經過view模塊,要呈現到前端的數據

Django view 的配置django

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、將view中的數據渲染到前端模版上

前端分頁代碼塊:bash

<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>
複製代碼

4、最終顯示效果

5ef36ee0c9dd799d8a534859e45a5728

(圖片來源於網絡)網絡

做者:任宏利函數

來源:宜信技術學院spa

相關文章
相關標籤/搜索