在django模板中生成分页链接
django-smart-pagination的Python项目详细描述
为django生成分页链接
Django智能分页不是一次显示所有页面的链接,而是计算它们的有限子集。
使用django模板和jinja2。
用法
首先将smart_pagination添加到已安装的应用:
INSTALLED_APPS=['smart_pagination']
使用分页块传递Page对象,即链接数(变量的文本) 以及关联smart_pagination.Paginator对象的名称。
{%loadpagination_tags%}{%paginatepage_objnum_linkspaging%} <ul> {%forpageinpaging.pages%} <li class="{%ifpage.is_current%}current{%endif%}">{{page.number}}</li> {%endfor%} </ul> {%endpaginate%}
Paginator对象包含以下属性:
first | First ^{tt2}$. Will be ^{tt6}$ if it is already the first page. |
prev | Previous ^{tt2}$. Will be ^{tt6}$ if there is no previous page. |
pages | List of pages. |
next | Next ^{tt2}$. Will be ^{tt6}$ if there is no next page. |
last | Last ^{tt2}$. Will be ^{tt6}$ if it is already the last page. |
{%loadpagination_tags%}{%paginatepage_objnum_linkspaging%} <ul> {%ifpaging.first%} <li>First - {{paging.first.number}}</li> {%endif%}{%ifpaging.prev%} <li>Previous - {{paging.prev.number}}</li> {%endif%}{%forpageinpaging.pages%} <li class="{%ifpage.is_current%}current{%endif%}">{{page.number}}</li> {%endfor%}{%ifpaging.next%} <li>Next - {{paging.next.number}}</li> {%endif%}{%ifpaging.last%} <li>Last - {{paging.last.number}}</li> {%endif%} </ul> {%endpaginate%}
如果将page_kwarg作为查询参数发送,则可以选择传递第四个名为 页面和Paginator将提供不带页面的查询字符串:
{%loadpagination_tags%}{%paginatepage_objnum_linkspaging'page'%} <ul> {%forpageinpaging.pages%} <li><a href="?page={{page.number}}&{{paging.query}}">{{page.number}}</a></li> {%endfor%} </ul> {%endpaginate%}