実際には、django スニペットの周りにたくさんの答えが浮かんでいます。
この投稿に出くわしました:
http://www.tummy.com/articles/django-pagination/
それは私が望んでいた最終結果をもたらしました。ジェネリック クラス ベースのビューで動作するように、または少なくとも同じコンテキスト変数が利用可能な場合に動作するように変更する必要がありました。
これは改善できると確信していますが、このままではかなり再利用できると思います。
- 最初に新しいアプリを作成します。例: my_pagination
- このアプリが設定で INSTALLED_APPS に追加されていることを確認してください
- このアプリ フォルダーに、templatetags と templates の 2 つのフォルダーを作成します。
- templatetags フォルダに paginator.py というファイルを作成します
- templates フォルダーに (この場合は 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のコードは、ロジックを理解していれば何でもかまいません (これは非常に簡単です)。Foundation 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.関連性がないように思われたため、元のパラメーターの多くを削除しました。