本文介绍了Django分页-来自文档的示例。如何显示所有站点编号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是来自文档Django的示例:

This is example from documentation 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>

此显示例如:

如何以这种方式显示它:

How to display it in this way:

previous  1 <b>2</b> 3 Next

当前页面和html < b> 标记。

Current page with html <b> mark.

推荐答案

您可以尝试以下操作:

You can try this:

{% 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分页-来自文档的示例。如何显示所有站点编号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 13:39