gpt4 book ai didi

Django 分页 - 文档中的示例。如何显示所有站点编号?

转载 作者:行者123 更新时间:2023-12-04 13:00:42 25 4
gpt4 key购买 nike

这是文档 Django 中的示例:

def listing(request):
contact_list = Contacts.objects.all()
paginator = Paginator(contact_list, 25) # Show 25 contacts per page

page = request.GET.get('page')
try:
contacts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
contacts = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
contacts = paginator.page(paginator.num_pages)

return render_to_response('list.html', {"contacts": contacts})

模板:
{% 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>

此显示例如:

Page 2 of 3. next



如何以这种方式显示它:
previous  1 <b>2</b> 3 Next

当前页面带有 html <b>标记。

?

最佳答案

你可以试试这个:

{% for num in contacts.paginator.page_range %}
{% ifequal num contacts.number %}
<span class="current"><b>{{ num }}</b></span>
{% else %}
<a href="?page={{ num }}"> {{ num }}</a>
{% endifequal %}
{% endfor %}

关于Django 分页 - 文档中的示例。如何显示所有站点编号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17490942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com