15

我的分页模块大部分时间都在工作,但有一个问题。我怎样才能只显示总可用页面的一部分。例如,假设我在 n 页中的第 5 页上,我想显示。

1,2,3,4,5,6....(n-1)(n)。

我相信 Ruby 有一些花哨的前端魔法来实现这一点。我可以在 django 模块中做些什么吗?

从其他站点看,似乎逻辑基本上是,选择固定数量的点。如果该数字大于当前显示的数量,请在用户向前翻页时添加项目。达到该点数后,仅在当前页面的左侧和右侧显示 x 个点。

我应该只编写自己的模板逻辑来完成吗?

谢谢

4

4 回答 4

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/

于 2011-03-25T02:30:50.010 回答
7

我刚刚使用Django Snippets 中的“Digg-like Paginator”实现了这一点。

这绝对是您正在寻找的。

只需将该代码转储到 Python 模块中digg_paginator.py,然后像这样导入它......

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 内置的完全相同的界面以及一些功能,因此您可以将其放置到位,然后根据需要进行一些自定义调整。

于 2011-03-25T18:48:23.663 回答
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”会修改分页截断的方式。您可以在我的网站上看到一个示例(页面右上角)。

于 2011-03-25T04:48:51.740 回答
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>&hellip;</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>&hellip;</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>
于 2017-09-27T10:44:04.390 回答