django纯分页提供了高级分页功能,并且与基于django核心分页模块的现有代码完全兼容。(又名不需要重写代码!)
django-pure-pagination的Python项目详细描述
说明
Author: | James Pacileo @ignighted |
---|---|
Version: | 0.3.0 |
Description: | django-pure-pagination provides advanced pagination features and is fully compatible with existing code based on Django’s core pagination module. (aka no need to rewrite code!) |
Requirements: | Django 1.7+ |
Contributors: | juandecarrion (Juande Carrion), twidi (Stéphane Angel), bebraw (Juho Vepsäläinen), lampslave (), GeyseR (Sergey Fursov), zeus (Pavel Zhukov) |
简介
Django应用程序提供了高级分页功能,而不强制在现有项目中更改主要代码。django纯分页是基于Django的核心分页模块,因此与现有API兼容。
Documentation for Django core pagination module
功能
- 开箱即用HTML呈现分页
- 其他方法使呈现更高级的分页模板更容易。
安装
从pypi安装包:
pip install django-pure-pagination
或从存储库克隆并安装:
git clone git@github.com:jamespacileo/django-pure-pagination.git cd django-pure-pagination python setup.py install
将纯分页添加到已安装的应用程序中
INSTALLED_APPS = ( ... 'pure_pagination', )
最后将django.core.paginator import paginator的替换为纯分页import paginator的
设置
可以在settings.py中设置一些设置
PAGINATION_SETTINGS = { 'PAGE_RANGE_DISPLAYED': 10, 'MARGIN_PAGES_DISPLAYED': 2, 'SHOW_FIRST_PAGE_WHEN_INVALID': True, }
page_range_displayed是将要显示的与当前页相邻的页数(默认值为10)
margin_pages_displayed是将显示的第一页和最后一页相邻的页数(默认值为2)
设置show_first_page_when_invalid为true,以便在提供无效页时仅显示第一页,而不是404错误
使用示例
下面是基于函数的视图的一个简单示例。有关基于类的通用视图,请参见下文。
查看文件:views.py
# views.py from django.shortcuts import render_to_response from pure_pagination import Paginator, EmptyPage, PageNotAnInteger def index(request): try: page = request.GET.get('page', 1) except PageNotAnInteger: page = 1 objects = ['john', 'edward', 'josh', 'frank'] # Provide Paginator with the request object for complete querystring generation p = Paginator(objects, request=request) people = p.page(page) return render_to_response('index.html', { 'people': people, }
模板文件:index.html
{# index.html #} {% extends 'base.html' %} {% block content %} {% for person in people.object_list %} <div> First name: {{ person }} </div> {% endfor %} {# The following renders the pagination html #} <div id="pagination"> {{ people.render }} </div> {% endblock %}
使用量
有几种不同的方法可以利用django pure pagination中引入的特性。
呈现分页的最简单方法是调用呈现方法,即{{page.render}
或者,您也可以自己访问页面对象低级方法
特别注意:页面对象和当前页面都指向模板中的页面对象。
{% load i18n %} <div class="pagination"> {% if page_obj.has_previous %} <a href="?{{ page_obj.previous_page_number.querystring }}" class="prev">‹‹ {% trans "previous" %}</a> {% else %} <span class="disabled prev">‹‹ {% trans "previous" %}</span> {% endif %} {% for page in page_obj.pages %} {% if page %} {% ifequal page page_obj.number %} <span class="current page">{{ page }}</span> {% else %} <a href="?{{ page.querystring }}" class="page">{{ page }}</a> {% endifequal %} {% else %} ... {% endif %} {% endfor %} {% if page_obj.has_next %} <a href="?{{ page_obj.next_page_number.querystring }}" class="next">{% trans "next" %} ››</a> {% else %} <span class="disabled next">{% trans "next" %} ››</span> {% endif %} </div>
基于类的通用视图
django基于类的视图的文档https://docs.djangoproject.com/en/dev/ref/class-based-views/
查看文件:
views.py
# views.py from django.views.generic import ListView from pure_pagination.mixins import PaginationMixin from my_app.models import MyModel class MyModelListView(PaginationMixin, ListView): # Important, this tells the ListView class we are paginating paginate_by = 10 # Replace it for your model or use the queryset attribute instead object = MyModel
模板文件:
请注意,基于django泛型的列表视图将在上下文中包含对象page_obj。有关https://docs.djangoproject.com/en/dev/ref/generic-views/#list-detail-generic-views的详细信息
_分页.html
{% load i18n %} <div class="pagination"> {% if page_obj.has_previous %} <a href="?{{ page_obj.previous_page_number.querystring }}" class="prev">‹‹ {% trans "previous" %}</a> {% else %} <span class="disabled prev">‹‹ {% trans "previous" %}</span> {% endif %} {% for page in page_obj.pages %} {% if page %} {% ifequal page page_obj.number %} <span class="current page">{{ page }}</span> {% else %} <a href="?{{ page.querystring }}" class="page">{{ page }}</a> {% endifequal %} {% else %} ... {% endif %} {% endfor %} {% if page_obj.has_next %} <a href="?{{ page_obj.next_page_number.querystring }}" class="next">{% trans "next" %} ››</a> {% else %} <span class="disabled next">{% trans "next" %} ››</span> {% endif %} </div>
my_app/myobject_list.html
{# my_app/myobject_list.html #} {% extends 'base.html' %} {% block content %} {% for object in object_list %} <div> First name: {{ object.first_name }} </div> {% endfor %} {# The following renders the pagination html #} {% include "_pagination.html" %} {% endblock %}