好吧,实际上有很多答案漂浮在 django 片段中。
我遇到了这篇文章:
http ://www.tummy.com/articles/django-pagination/
这有我想要的最终结果。我只需要修改它,以便它可以与基于通用类的视图一起使用,或者至少在相同的上下文变量可用时。
我确信这可以改进,但事实上,我认为这是非常可重用的。
- 首先创建一个新的应用程序。例如 my_pagination
- 确保在您的设置中将此应用添加到 INSTALLED_APPS
- 在这个 app 文件夹中创建两个文件夹:templatetags 和 templates
- 在 templatetags 文件夹中创建一个名为 paginator.py 的文件
- 在模板文件夹中创建一个名为(在本例中为 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. 我从原版中取出了许多参数,因为它们似乎不相关。