A Better Pagination Component Than Django's Official Implementation + Bootstrap Integration
Preface The pagination component that comes with Django can only be said to fulfill the pagination function, but it's just not that easy to use Django's pagination effect django-pure-pagination pagination effect Usage First, install: pip install django-pure-pagination Add to App INSTALLED_APPS = ( ... 'pure_pagination', ) Configure it; the configuration instructions are as shown in the figure P
目录10 个章节
Preface
The pagination component that comes with Django can only be said to fulfill the pagination function, but it's just not that easy to use
Django's pagination effect

django-pure-pagination pagination effect

Usage
First, install:
pip install django-pure-pagination
Add to App
INSTALLED_APPS = (
...
'pure_pagination',
)
Configure it; the configuration instructions are as shown in the figure

PAGINATION_SETTINGS = {
'PAGE_RANGE_DISPLAYED': 10,
'MARGIN_PAGES_DISPLAYED': 2,
'SHOW_FIRST_PAGE_WHEN_INVALID': True,
}
View code example
from django.shortcuts import render
from pure_pagination import Paginator
from .models import Video
def all_video(request):
video_list = Video.objects.all()
paginator_obj = Paginator(video_list, 10, request=request)
ctx = {
'page': paginator_obj.page(request.GET.get('page', 1)),
}
return render(request, 'video_list.html', context=ctx)
Template code
Note that the Paginator object of django-pure-pagination is a bit different from Django's built-in Paginator; they differ in usage.
Django's built-in Paginator is itself an iterator, so you can just for item in page, but django-pure-pagination is different—you have to for item in page.object_list. Mind this difference~
Therefore, django-pure-pagination needs to iterate over the objects on the page like this:
{% for video in page.object_list %}
{% include "widgets/video_card.html" %}
{% empty %}
<div class="alert alert-secondary" role="alert">
视频列表获取为空!
</div>
{% endfor %}
Paginator HTML code
It's best to write a separate paginator.html as a Django template component, then other pages can conveniently include it with {% includ "paginator.html" %}
{% load i18n %}
{% if page.paginator.num_pages > 1 %}
<nav aria-label="Page navigation example">
<ul class="pagination justify-content-center">
{% if page.has_previous %}
<li class="page-item">
<a class="page-link" href="?{{ page.previous_page_number.querystring }}">‹‹ {% trans "previous" %}</a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link">‹‹ {% trans "previous" %}</span>
</li>
{% endif %}
{% for p in page.pages %}
{% if p %}
{% ifequal p page.number %}
<li class="page-item active">
<span class="page-link">{{ p }}</span>
</li>
{% else %}
<li class="page-item">
<a class="page-link" href="?{{ p.querystring }}">{{ p }}</a>
</li>
{% endifequal %}
{% else %}
<li class="page-item disabled">
<span class="page-link">...</span>
</li>
{% endif %}
{% endfor %}
{% if page.has_next %}
<li class="page-item">
<a href="?{{ page.next_page_number.querystring }}" class="page-link">{% trans "next" %} ››</a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link disabled">{% trans "next" %} ››</span>
</li>
{% endif %}
</ul>
</nav>
{% endif %}
Final result

References
- Official website: https://github.com/jamespacileo/django-pure-pagination/
Welcome to connect
Programming Lab focuses on exploring popular new internet technologies and practicing team agile development. Reply with linux, flutter, c#, netcore, android, kotlin, java, python, etc. in the backend of the WeChat official account “Programming Lab” to get related technical articles and materials. If you have any questions, you can also leave a message in the official account backend~

- Cnblogs: https://www.cnblogs.com/deali/
- Coding livestream: https://live.bilibili.com/11883038
- Zhihu: https://www.zhihu.com/people/dealiaxy
_EOF_
A Better Pagination Component Than Django's Official Implementation + Bootstrap Integration
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
Django中「强行」使用MVVM设计模式
扫盲 首先带大家了解一下什么是MVVM模式: 什么是MVVM?MVVM是Model-View-ViewModel的缩写。 MVVM是MVC的增强版,实质上和MVC没有本质区别,只是代码的位置变动而已 从名字上看,MVVM比MVC架构中多了一个ViewModel,没错,就是这个ViewModel,他是MVVM相对于MVC改进的核心思想。在开发过程中,由于需求的变更或添加,项目的复杂度越来越高,代码量
2021-12-18 · Django · 约 1 分钟
Django应用部署:Docker容器化实践
在此之前,我一直用uwsgi+virtualenv+nginx方式进行应用部署,操作起来比较麻烦,而且依赖于服务器上的Python版本,服务的管理方面单纯uwsgi + pid算不上特别麻烦但总没有docker compose方便,无奈原本的服务器是32位系统的用不上docker,现在又增加了一个新服务器,所以赶紧把docker安排上~ 不得不说真的太简单太方便了,全程傻瓜式操作,也基本没像之前u
2021-12-18 · Django · 约 1 分钟
分享一些2026年有意思的现代化Django生态组件
2026年了,若还在忍受石器时代的模板和冗余CRUD,趁早转行。Cotton和Inertia总算给Django这老古董续了命,Ninja这类工具已是开发及格线。看一眼这些现代化组件吧,别再用那些自我感动的低效代码来折磨审美了。
2026-01-21 · Django · 约 1 分钟
设置Django关闭Debug后的静态文件路由
Django在Debug模式关闭掉后请求静态文件时,返回404相应码,后台的请求url是"GET /static/css/404.css HTTP/1.1" 404 1217,找不到静态文件的url。通过配置静态文件的url和命令行启动来实现静态文件的获取。 URL中的配置 django的静态文件是通过``来管理的。配置静态文件的响应是通过serve()函数数显的。url.py中的配置如下: fr
2021-12-18 · Django · 约 1 分钟
讨论
共 0 条加载评论中…