先看这里:
http://blog.csdn.net/kunshan_shenbin/article/details/7687134
http://tonylandis.com/pylons/couchdb/couchdbkit/python/html-pagination-for-couchdbkit/
Django有很多的可选翻页组件,可惜否是基于关系型数据库的。
以下代码基于Django/CouchDBKit
http://blog.csdn.net/kunshan_shenbin/article/details/7671066
在greeting目录下新建paginator.py,代码如下(改写了Django自带的翻页组件)
from math import ceil
class InvalidPage(Exception):
pass
class PageNotAnInteger(InvalidPage):
pass
class EmptyPage(InvalidPage):
pass
class Paginator(object):
def __init__(self, object_list, per_page, total_rows, orphans=0, allow_empty_first_page=True):
self.object_list = object_list
self.per_page = int(per_page)
self.orphans = int(orphans)
self.allow_empty_first_page = allow_empty_first_page
self._num_pages = self._count = None
self.total_rows = int(total_rows)
def validate_number(self, number):
"Validates the given 1-based page number."
try:
number = int(number)
except (TypeError, ValueError):
raise PageNotAnInteger('That page number is not an integer')
if number < 1:
raise EmptyPage('That page number is less than 1')
if number > self.num_pages:
if number == 1 and self.allow_empty_first_page:
pass
else:
raise EmptyPage('That page contains no results')
return number
def page(self, number):
"Returns a Page object for the given 1-based page number."
number = self.validate_number(number)
'''
bottom = (number - 1) * self.per_page
top = bottom + self.per_page
if top + self.orphans >= self.count:
top = self.count
return Page(self.object_list[bottom:top], number, self)
'''
return Page(self.object_list, number, self)
def _get_count(self):
"Returns the total number of objects, across all pages."
if self._count is None:
try:
self._count = self.total_rows
except (AttributeError, TypeError):
# AttributeError if object_list has no count() method.
# TypeError if object_list.count() requires arguments
# (i.e. is of type list).
self._count = len(self.object_list)
return self._count
count = property(_get_count)
def _get_num_pages(self):
"Returns the total number of pages."
if self._num_pages is None:
if self.count == 0 and not self.allow_empty_first_page:
self._num_pages = 0
else:
hits = max(1, self.count - self.orphans)
self._num_pages = int(ceil(hits / float(self.per_page)))
return self._num_pages
num_pages = property(_get_num_pages)
def _get_page_range(self):
"""
Returns a 1-based range of pages for iterating through within
a template for loop.
"""
return range(1, self.num_pages + 1)
page_range = property(_get_page_range)
QuerySetPaginator = Paginator # For backwards-compatibility.
class Page(object):
def __init__(self, object_list, number, paginator):
self.object_list = object_list
self.number = number
self.paginator = paginator
def __repr__(self):
return '<Page %s of %s>' % (self.number, self.paginator.num_pages)
def __len__(self):
return self.total_rows
def __getitem__(self, index):
# The object_list is converted to a list so that if it was a QuerySet
# it won't be a database hit per __getitem__.
return list(self.object_list)[index]
# The following four methods are only necessary for Python <2.6
# compatibility (this class could just extend 2.6's collections.Sequence).
def __iter__(self):
i = 0
try:
while True:
v = self[i]
yield v
i += 1
except IndexError:
return
def __contains__(self, value):
for v in self:
if v == value:
return True
return False
def index(self, value):
for i, v in enumerate(self):
if v == value:
return i
raise ValueError
def count(self, value):
return sum([1 for v in self if v == value])
# End of compatibility methods.
def has_next(self):
return self.number < self.paginator.num_pages
def has_previous(self):
return self.number > 1
def has_other_pages(self):
return self.has_previous() or self.has_next()
def next_page_number(self):
return self.number + 1
def previous_page_number(self):
return self.number - 1
def start_index(self):
"""
Returns the 1-based index of the first object on this page,
relative to total objects in the paginator.
"""
# Special case, return zero if no items.
if self.paginator.count == 0:
return 0
return (self.paginator.per_page * (self.number - 1)) + 1
def end_index(self):
"""
Returns the 1-based index of the last object on this page,
relative to total objects found (hits).
"""
# Special case for the last page because there can be orphans.
if self.number == self.paginator.num_pages:
return self.paginator.count
return self.number * self.paginator.per_page
修改views.py
from django.views.decorators.csrf import csrf_protect
from django.shortcuts import render_to_response as render
from django.template import RequestContext
from couchdbkit.ext.django.forms import DocumentForm
from greeting.models import Greeting
from greeting.paginator import Paginator, EmptyPage, PageNotAnInteger
class GreetingForm(DocumentForm):
class Meta:
document = Greeting
@csrf_protect
def home(request):
greet = None
if request.POST:
form = GreetingForm(request.POST)
if form.is_valid():
greet = form.save()
else:
form = GreetingForm()
limit = 5
page = request.GET.get('page')
if page == None: page = 1
skip = (int(page) - 1) * limit
greetings = Greeting.view('greeting/all', descending=False, skip=skip, limit=limit)
total_rows = Greeting.view('greeting/all', limit=0).total_rows
p = Paginator(greetings, limit, total_rows)
try:
paginator = p.page(page)
except PageNotAnInteger:
paginator = p.page(1)
except EmptyPage:
paginator = p.page(1)
return render("home.html", {
"form": form,
"greet": greet,
"greetings": greetings,
"paginator": paginator,
}, context_instance=RequestContext(request))
修改template文件home.html
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<form method="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" id="submit" value="submit">
</form>
{% if greet %}
{{ greet.get_id }} was added
{% endif %}
<h2>Greetings</h2>
<table>
{% for g in greetings %}
<tr>
<td>{{ g.id }} </td>
<td>{{ g.date }} </td>
<td>{{ g.author }} </td>
<td>{{ g.content }}</td>
</tr>
{% endfor %}
</table>
<div class="pagination">
<span class="step-links">
{% if paginator.has_previous %}
<a href="?page={{ paginator.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ paginator.number }} of {{ paginator.paginator.num_pages }}.
</span>
{% if paginator.has_next %}
<a href="?page={{ paginator.next_page_number }}">next</a>
{% endif %}
</span>
</div>
{% endblock content %}
原理:使用limit和skip参数来限制每次查询取得的行数。
可惜的是这个方法在CouchDB官方不被推荐,原因是skip永远都是从记录的第一行开始进行偏移取值,当skip数字很大时会影响性能。
http://guide.couchdb.org/editions/1/en/recipes.html
当然上文中也讲述了一个推荐的方案,看着有点别扭,等下次有空的时候再把细节阐述一下了。