如何在 Django 分页器中使用 get_elided_page_range?
How to use get_elided_page_range in Django paginator?
有创建分页范围的新选项 - get_elided_page_range
https://docs.djangoproject.com/en/3.2/ref/paginator/#django.core.paginator.Paginator.get_elided_page_range
我应该如何使用它?我如何设置参数?我正在使用 CBV ListView。
我尝试了 https://nemecek.be/blog/105/how-to-use-elided-pagination-in-django-and-solve-too-many-pages-problem 但它对我不起作用。
我有 81 页,当前页是 10。问题是我的范围总是 1 2 3 4 ... 80 81
我做错了什么?
#views.py
class TrailersListView(ListView):
queryset = Trailer.objects.all()
paginate_by = 10
#template.html
{% for i in paginator.get_elided_page_range %}
{% if page_obj.number == i %}
<li class="active page-item">
<span class="page-link">{{ i }}</span>
</li>
{% else %}
{% if i == paginator.ELLIPSIS %}
<li class="page-item">
<span class="page-link">{{ paginator.ELLIPSIS }}</span>
</li>
{% else %}
<li class="page-item">
<a class="page-link" href="?page={{ i }}">{{ i }}</a>
</li>
{% endif %}
{% endif %}
{% endfor %}
页码不会传递给“get_elided_page_range”方法,它始终使用默认值 (1)。所以我创建了一个自定义标签来传递一个数字。
appname/templatetags/paginator_tags.py
from django import template
from django.core.paginator import Paginator
register = template.Library()
@register.simple_tag
def get_proper_elided_page_range(p, number, on_each_side=3, on_ends=2):
paginator = Paginator(p.object_list, p.per_page)
return paginator.get_elided_page_range(number=number,
on_each_side=on_each_side,
on_ends=on_ends)
template.html
{% load paginator_tags %}
{% get_proper_elided_page_range paginator page_obj.number as page_range %} <!-- here -->
{% for page_num in page_range %}
{% if page_obj.number == i %}
<li class="active page-item">
<span class="page-link">{{ i }}</span>
</li>
{% else %}
{% if i == paginator.ELLIPSIS %}
<li class="page-item">
<span class="page-link">{{ paginator.ELLIPSIS }}</span>
</li>
{% else %}
<li class="page-item">
<a class="page-link" href="?page={{ i }}">{{ i }}</a>
</li>
{% endif %}
{% endif %}
{% endfor %}
您可以添加额外的参数,例如:
{% get_proper_elided_page_range paginator page_obj.number 1 1 as page_range %}
有创建分页范围的新选项 - get_elided_page_range https://docs.djangoproject.com/en/3.2/ref/paginator/#django.core.paginator.Paginator.get_elided_page_range
我应该如何使用它?我如何设置参数?我正在使用 CBV ListView。 我尝试了 https://nemecek.be/blog/105/how-to-use-elided-pagination-in-django-and-solve-too-many-pages-problem 但它对我不起作用。
我有 81 页,当前页是 10。问题是我的范围总是 1 2 3 4 ... 80 81 我做错了什么?
#views.py
class TrailersListView(ListView):
queryset = Trailer.objects.all()
paginate_by = 10
#template.html
{% for i in paginator.get_elided_page_range %}
{% if page_obj.number == i %}
<li class="active page-item">
<span class="page-link">{{ i }}</span>
</li>
{% else %}
{% if i == paginator.ELLIPSIS %}
<li class="page-item">
<span class="page-link">{{ paginator.ELLIPSIS }}</span>
</li>
{% else %}
<li class="page-item">
<a class="page-link" href="?page={{ i }}">{{ i }}</a>
</li>
{% endif %}
{% endif %}
{% endfor %}
页码不会传递给“get_elided_page_range”方法,它始终使用默认值 (1)。所以我创建了一个自定义标签来传递一个数字。
appname/templatetags/paginator_tags.py
from django import template
from django.core.paginator import Paginator
register = template.Library()
@register.simple_tag
def get_proper_elided_page_range(p, number, on_each_side=3, on_ends=2):
paginator = Paginator(p.object_list, p.per_page)
return paginator.get_elided_page_range(number=number,
on_each_side=on_each_side,
on_ends=on_ends)
template.html
{% load paginator_tags %}
{% get_proper_elided_page_range paginator page_obj.number as page_range %} <!-- here -->
{% for page_num in page_range %}
{% if page_obj.number == i %}
<li class="active page-item">
<span class="page-link">{{ i }}</span>
</li>
{% else %}
{% if i == paginator.ELLIPSIS %}
<li class="page-item">
<span class="page-link">{{ paginator.ELLIPSIS }}</span>
</li>
{% else %}
<li class="page-item">
<a class="page-link" href="?page={{ i }}">{{ i }}</a>
</li>
{% endif %}
{% endif %}
{% endfor %}
您可以添加额外的参数,例如:
{% get_proper_elided_page_range paginator page_obj.number 1 1 as page_range %}