Django实现翻页功能很简单,直接使用Django 自带的Paginator模块就可以实现

源代码:

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from website.myadd.models import item

def list(request):
    item_list = item.objects.values("img_url","num_iid").filter(num_iid__isnull=False).order_by("-id")
    paginator = Paginator(item_list,50)
    p = request.GET.get('page','')

    try:
        items = paginator.page(p)
    except PageNotAnInteger:
        items = paginator.page(1)
    except EmptyPage:
        items = paginator.page(paginator.num_pages)
    return render_to_response('list.html',{'items': items})

翻页模版:

{% block content %}
    {% for item in items.object_list %}
               item.img
    {% endfor %}

        {% if items.has_previous %}
            previous
        {% endif %}

            Page {{ items.number }} of {{ items.paginator.num_pages }}.

        {% if items.has_next %}
            next
        {% endif %}