2

(这是我分享一个解决方案,我在尝试创建页面导航时遇到了这个基本问题)

这似乎是一件非常基本的事情,我无法相信找到一个基本的解决方案是如此困难。

基本上 django 中的分页非常好,直到您需要在模板中将页面显示为导航。然后你必须创建一些淫秽的逻辑来完成以下

  • 显示与当前页相邻的 x 页码。例如 2 之前和 2 之后
  • 确定何时显示第一页码
  • 确定何时显示最后页码
  • 突出显示当前页码
  • 根据你想要的风格

对于少量页面,默认分页很好,但是一旦你有一个重要的数字,就很难完成上述要求。

我已经发布了一个我找到并修改为与 django 1.5+ 一起使用的解决方案。如果您有其他解决方案可以围绕这个主题分享,很乐意看到它们。谢谢

4

2 回答 2

1

好吧,实际上有很多答案漂浮在 django 片段中。
我遇到了这篇文章: http ://www.tummy.com/articles/django-pagination/

这有我想要的最终结果。我只需要修改它,以便它可以与基于通用类的视图一起使用,或者至少在相同的上下文变量可用时。

我确信这可以改进,但事实上,我认为这是非常可重用的。

  1. 首先创建一个新的应用程序。例如 my_pagination
  2. 确保在您的设置中将此应用添加到 INSTALLED_APPS
  3. 在这个 app 文件夹中创建两个文件夹:templatetags 和 templates
  4. 在 templatetags 文件夹中创建一个名为 paginator.py 的文件
  5. 在模板文件夹中创建一个名为(在本例中为 my_pagination)的子文件夹并添加一个名为 _paginator.html 的文件。(在文件名前加下划线是我自己对包含的模板文件的约定)

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/

# modified again by me to include target_url and work with django 1.7

from django import template
from django.core.paginator import InvalidPage

register = template.Library()

def paginator(context, adjacent_pages=2, target_url=None):
    """
    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']

    try:
        previous = context['page_obj'].previous_page_number()
    except InvalidPage:
        previous = None

    try:
        next = context['page_obj'].next_page_number()
    except InvalidPage:
        next = None


    return {
        'page_obj': page_obj,
        'paginator': paginator,
        'page': context['page_obj'].number,
        'page_numbers': page_numbers,
        'next': next,
        'previous': previous,
        'has_next': context['page_obj'].has_next(),
        'has_previous': context['page_obj'].has_previous(),
        'show_first': 1 not in page_numbers,
        # show last if the last page number is not in "page_numbers"
        'show_last': context['paginator'].num_pages not in page_numbers,
        'target_url' : target_url
    }

register.inclusion_tag('my_pagination/_paginator.html', takes_context=True)(paginator)

_paginator.html的代码可以是任何你想要的,只要你理解逻辑(这很简单)我使用基础 5 和 fontawesome 所以我的 _paginator.html 看起来像

<ul class="pagination">
   {% if has_previous %}
   <li class="arrow"><a href="{{ target_url }}?page={{ previous }}" target="_parent"><i class="fa fa-angle-double-left fa-lg fa-fw"></i></a></li>
   {% else %}
   <li class="arrow unavailable"><i class="fa fa-angle-double-left fa-lg fa-fw"></i></li>
   {% endif %}

   {% if show_first %}
      <li><a href="{{ target_url }}?page=1" target="_parent">1</a></li>
      <li>...</li>
   {% endif %}

   {% for linkpage in page_numbers %}
      {% if linkpage == page %}
          <li class="current">{{ page }}</li>
      {% else %}
         <li><a href="{{ target_url }}?page={{ linkpage }}" target="_parent">{{ linkpage }}</a></li>
      {% endif %}
   {% endfor %}

  {% if show_last %}
      <li>...</li>
      <li><a href="{{ target_url }}?page={{ paginator.num_pages }}" target="_parent">{{ paginator.num_pages }}</a></li>
   {% endif %}
   {% if has_next %}
       <li class="arrow"><a href="{{ target_url }}?page={{ next }}" target="_parent"><i class="fa fa-angle-double-right fa-lg fa-fw"></i></a></li>
   {% else %}
      <li class="arrow unavailable"><i class="fa fa-angle-double-right fa-lg fa-fw"></i></li>
   {% endif %}
</ul>

如何使用: 在您的模板文件中确保您这样做:

{% load paginator %}

加载自定义模板标签

然后您希望页面显示在哪里,您只需执行以下操作:

{% paginator 3 %}

注释
1. target_url 仅仅是因为我对 iframe 有一些奇怪的要求。您可以简单地将此参数留空。
2. 我从原版中取出了许多参数,因为它们似乎不相关。

于 2014-08-26T06:54:14.483 回答
1

Django 为您提供了在模板中使用 paginator 对象的基础知识:

{% for contact in contacts %}
    {# Each "contact" is a Contact model object. #}
    {{ contact.full_name|upper }}<br />
    ...
{% endfor %}

<div class="pagination">
    <span class="step-links">
        {% if contacts.has_previous %}
            <a href="?page={{ contacts.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}.
        </span>

        {% if contacts.has_next %}
            <a href="?page={{ contacts.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>

这没有包含在标签中的原因可能是因为 django 不想对您强制执行某种样式/库(jquery 除外,但即使是可选的)。

进一步决定分页的风格可能是自以为是的,并且与应用程序和视口不同。但是,还有很多其他人已经解决了这个问题。立即想到的两个是django-endless-paginationdjango-bootstrap-pagination

于 2014-08-26T06:51:43.933 回答