分页不适用于中间的数字,也不适用于 Django 中的反向数字
pagination does not work for numbers in between and also for reverse in django
我正在过滤我的数据,还应用了分页器,我找到了一些解决方案并使用了它,但它仅适用于下一个按钮,而不适用于上一个按钮和介于两者之间的按钮编号,即 {{i}} (Django, python)
这是 template_tag 文件
myapp_extra.py
from django import template
register = template.Library()
@register.simple_tag
def my_url(value, field_name, urlencode=None):
url = '?{}={}'.format(field_name, value)
if urlencode:
querystring = urlencode.split('&')
filterd_querystring = filter(lambda p: p.split('=')[0]!=field_name, querystring)
encoded_querystring = '&'.join(filterd_querystring)
url = '{}&{}'.format(url, encoded_querystring)
return url
在我的 html
{% load myapp_extra %}
<other code>
<nav>
<ul class="pagination rounded-separated justify-content-center" style="float: right; margin-top: 1%;">
{% if employees_attendance_list.has_previous %}
<li class="page-item"><a class="page-link" href="{% my_url employees_attendance_list.previous_page_number 'page' request.GET.urlencode }"><i class="mdi mdi-chevron-left"></i></a></li>
{% else %}
<li class="disabled"></li>
{% endif %}
{% for i in employees_attendance_list.paginator.page_range %}
{% if employees_attendance_list.number == i %}
<li class="page-item active"><a class="page-link" >{{ i }}</a></li>
{% else %}
<li class="page-item"><a class="page-link" href="{% my_url i 'page' request.GET.urlencode}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if employees_attendance_list.has_next %}
<li class="page-item"><a class="page-link" href="{% my_url employees_attendance_list.next_page_number 'page' request.GET.urlencode %}"><i class="mdi mdi-chevron-right"></i></a></li>
{% else %}
<li class="disabled"></li>
{% endif %}
</ul>
</nav>
观看次数
page = request.GET.get('page', 1)
paginator = Paginator(employees_attendance, settings.PAGE_LIMIT)
try:
employees_attendance_list = paginator.page(page)
except PageNotAnInteger:
employees_attendance_list = paginator.page(1)
except EmptyPage:
employees_attendance_list = paginator.page(paginator.num_pages)
我对 html
进行了一些更改
<nav>
<ul class="pagination rounded-separated justify-content-center" style="float: right; margin-top: 1%;">
{% for i in employees_attendance_list.paginator.page_range %}
<li class="page-item">
<a class="page-link" href="{% my_url i 'page' request.GET.urlencode %}">{{ i }}</a>
</li>
{% endfor %}
</ul>
</nav>
我正在过滤我的数据,还应用了分页器,我找到了一些解决方案并使用了它,但它仅适用于下一个按钮,而不适用于上一个按钮和介于两者之间的按钮编号,即 {{i}} (Django, python)
这是 template_tag 文件 myapp_extra.py
from django import template
register = template.Library()
@register.simple_tag
def my_url(value, field_name, urlencode=None):
url = '?{}={}'.format(field_name, value)
if urlencode:
querystring = urlencode.split('&')
filterd_querystring = filter(lambda p: p.split('=')[0]!=field_name, querystring)
encoded_querystring = '&'.join(filterd_querystring)
url = '{}&{}'.format(url, encoded_querystring)
return url
在我的 html
{% load myapp_extra %}
<other code>
<nav>
<ul class="pagination rounded-separated justify-content-center" style="float: right; margin-top: 1%;">
{% if employees_attendance_list.has_previous %}
<li class="page-item"><a class="page-link" href="{% my_url employees_attendance_list.previous_page_number 'page' request.GET.urlencode }"><i class="mdi mdi-chevron-left"></i></a></li>
{% else %}
<li class="disabled"></li>
{% endif %}
{% for i in employees_attendance_list.paginator.page_range %}
{% if employees_attendance_list.number == i %}
<li class="page-item active"><a class="page-link" >{{ i }}</a></li>
{% else %}
<li class="page-item"><a class="page-link" href="{% my_url i 'page' request.GET.urlencode}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if employees_attendance_list.has_next %}
<li class="page-item"><a class="page-link" href="{% my_url employees_attendance_list.next_page_number 'page' request.GET.urlencode %}"><i class="mdi mdi-chevron-right"></i></a></li>
{% else %}
<li class="disabled"></li>
{% endif %}
</ul>
</nav>
观看次数
page = request.GET.get('page', 1)
paginator = Paginator(employees_attendance, settings.PAGE_LIMIT)
try:
employees_attendance_list = paginator.page(page)
except PageNotAnInteger:
employees_attendance_list = paginator.page(1)
except EmptyPage:
employees_attendance_list = paginator.page(paginator.num_pages)
我对 html
进行了一些更改 <nav>
<ul class="pagination rounded-separated justify-content-center" style="float: right; margin-top: 1%;">
{% for i in employees_attendance_list.paginator.page_range %}
<li class="page-item">
<a class="page-link" href="{% my_url i 'page' request.GET.urlencode %}">{{ i }}</a>
</li>
{% endfor %}
</ul>
</nav>