我的分页模块大部分都在工作但有一个问题。我怎样才能显示总可用页面的一部分。例如,假设我在n页的第5页上,我想展示。
... 1,2,3,4,5,6-第(n-1)(n)的
我相信Ruby有一些奇特的前端魔术来实现这一目标。在django模块中我能做些什么吗?
从其他网站看,似乎逻辑基本上是,选择固定数量的点。如果该数字大于当前显示的数量,请在用户向前翻页时添加项目。达到该点数后,仅在当前页面的左侧和右侧显示x个点。
我应该编写自己的模板逻辑来实现这一目标吗?
由于
答案 0 :(得分:11)
很多关于在django中实现你要求的好文章。 如果您不想自己动手,可以自定义大量可插拔包。
http://www.tummy.com/Community/Articles/django-pagination/
https://github.com/dcramer/django-paging
非常好的包装内置分页器的文档片段:
http://blog.elsdoerfer.name/2008/03/06/yet-another-paginator-digg-style/
答案 1 :(得分:7)
我刚刚使用"Digg-like Paginator" from Django Snippets实现了这一点。
这绝对是你要找的。 p>
只需将代码转储到像digg_paginator.py
这样的Python模块中,然后像这样导入它......
from django.core.paginator import InvalidPage, EmptyPage #, Paginator
from digg_paginator import DiggPaginator as Paginator
然后在我的模板中,我不得不从...切换。
{% for page_num in page.paginator.page_range %}
到
{% for page_num in page.page_range %}
就是这样! DiggPaginator
具有与Django内置的完全相同的界面以及一些功能,因此您可以将其放置到位,然后在您认为需要时使用一些自定义进行调整。
答案 2 :(得分:2)
我最近不得不为我的网站Wantbox.com做同样的事情,并找到了一个非常简单有效的解决方案。
我在我的应用程序的“templatetags”目录中将此代码段添加到名为“paginator.py”的文件中:
# Based on: http://www.djangosnippets.org/snippets/73/
#
# Modified by Sean Reifschneider to be smarter about surrounding page
# link context. For usage documentation see:
#
# http://www.tummy.com/Community/Articles/django-pagination/
from django import template
register = template.Library()
def paginator(context, adjacent_pages=2):
"""
To be used in conjunction with the object_list generic view.
Adds pagination context variables for use in displaying first, adjacent and
last page links in addition to those created by the object_list generic
view.
"""
startPage = max(context['page'] - adjacent_pages, 1)
if startPage <= 3: startPage = 1
endPage = context['page'] + adjacent_pages + 1
if endPage >= context['pages'] - 1: endPage = context['pages'] + 1
page_numbers = [n for n in range(startPage, endPage) \
if n > 0 and n <= context['pages']]
page_obj = context['page_obj']
paginator = context['paginator']
return {
'page_obj': page_obj,
'paginator': paginator,
'hits': context['hits'],
'results_per_page': context['results_per_page'],
'page': context['page'],
'pages': context['pages'],
'page_numbers': page_numbers,
'next': context['next'],
'previous': context['previous'],
'has_next': context['has_next'],
'has_previous': context['has_previous'],
'show_first': 1 not in page_numbers,
'show_last': context['pages'] not in page_numbers,
}
register.inclusion_tag('paginator.html', takes_context=True)(paginator)
然后我将其添加到我想要分页的页面:
<div>{% if is_paginated %}{% load paginator %}{% paginator 3 %}{% endif %}</div>
将“3”更改为“2”或“4”会修改分页的截断方式。您可以在我的网站here上看到一个示例(页面右上角)。
答案 3 :(得分:0)
如果您使用的是ListView,这是您的分页器功能:
def paginator(context, adjacent_pages=2):
"""
To be used in conjunction with the object_list generic view.
Adds pagination context variables for use in displaying first, adjacent and
last page links in addition to those created by the object_list generic
view.
"""
startPage = max(context['page_obj'].number - adjacent_pages, 1)
if startPage <= 3: startPage = 1
endPage = context['page_obj'].number + adjacent_pages + 1
if endPage >= context['paginator'].num_pages - 1: endPage = context['paginator'].num_pages + 1
page_numbers = [n for n in range(startPage, endPage) \
if n > 0 and n <= context['paginator'].num_pages]
page_obj = context['page_obj']
paginator = context['paginator']
return {
'page_obj': page_obj,
'paginator': paginator,
'page': context['page_obj'].number,
'pages': context['paginator'].num_pages,
'page_numbers': page_numbers,
'next': context['page_obj'].next_page_number,
'previous': context['page_obj'].previous_page_number,
'has_next': context['page_obj'].has_next,
'has_previous': context['page_obj'].has_previous,
'show_first': 1 not in page_numbers,
'show_last': context['paginator'].num_pages not in page_numbers,
}
<nav>
<ul class="pagination">
<li{% if not page_obj.has_previous %} class="disabled"{% endif %}>
<a {% if page_obj.has_previous %} href="?{{ path }}page={{ page_obj.previous_page_number }}"{% endif %}><i
class="fa fa-angle-left"></i></a>
</li>
{% if show_first %}
<li><a href="?{{ path }}page=1">1</a></li>
<li class="disabled"><a>…</a></li>
{% endif %}
{% for linkpage in page_numbers %}
{% ifequal linkpage page %}
<li class="active"><a>{{ page }}</a></li>
{% else %}
<li><a href="?{{ path }}page={{ linkpage }}">{{ linkpage }}</a></li>
{% endifequal %}
{% endfor %}
{% if show_last %}
<li class="disabled"><a>…</a></li>
<li><a href="?{{ path }}page=last">{{ pages }}</a></li>
{% endif %}
<li{% if not page_obj.has_next %} class="disabled"{% endif %}>
<a {% if page_obj.has_next %} href="?{{ path }}page={{ page_obj.next_page_number }}"{% endif %}><i
class="fa fa-angle-right"></i></a>
</li>
</ul>
</nav>