1

我在这里找到了一个不错的分页模板。然而,这个例子是用 SQLlite 完成的,但我重写了以便能够使用 CouchDB。

此脚本创建用户并将其保存到 CouchDB:

from couchdb import Server
import sys

def fill_data(users_no):
      for i in range(users_no):
          doc = {
                  '_id': str(i),
                  'uname': "name_" + str(i),
                }
          db.save(doc)


if __name__ == '__main__':
    server = Server()
    db = server.create("pagination")

    fill_data(1000)

我修改了onrder 中的原始app.py脚本,以便能够使用 CouchDB。这是修改后的脚本:

from __future__ import unicode_literals
from flask import Flask, render_template, g, current_app, request
from flask.ext.paginate import Pagination
from couchdb import Server

app = Flask(__name__)
app.config.from_pyfile('app.cfg')

@app.route('/')
def index():
    """
    Map function
    function(doc) {
      emit(doc._id, doc.uname);
    }
    """
    for row in db.view('_design/snp/_view/by_unames'):
        print row

    total = ???
    print total
    page, per_page, offset = get_page_items()
    pagination = get_pagination(page=page,
                per_page=per_page,
                total=total,
                record_name=users,
                )
    return render_template('index.html', users=users,
              page=page,
              per_page=per_page,
              pagination=pagination,
              )


def get_css_framework():
    return current_app.config.get('CSS_FRAMEWORK', 'bootstrap3')


def get_link_size():
    return current_app.config.get('LINK_SIZE', 'sm')


def show_single_page_or_not():
    return current_app.config.get('SHOW_SINGLE_PAGE', False)


def get_page_items():
    page = int(request.args.get('page', 1))
    per_page = request.args.get('per_page')
    if not per_page:
            per_page = current_app.config.get('PER_PAGE', 10)
    else:
            per_page = int(per_page)

    offset = (page - 1) * per_page
    return page, per_page, offset


def get_pagination(**kwargs):
      kwargs.setdefault('record_name', 'records')
      return Pagination(css_framework=get_css_framework(),
          link_size=get_link_size(),
          show_single_page=show_single_page_or_not(),
          **kwargs
          )

if __name__ == '__main__':

    server = Server()
    db = server["pagination"]
    app.run(debug=True)
  • 从视图中获取总数的最佳方法是什么?

  • 有没有办法通过单击分页菜单中的下一页或上一页来避免重新计算总计数?

4

1 回答 1

1

CouchDB 使这很容易,因为它会自动计算视图中的文档总数。

让我们看一个示例,并在我们处理它的同时输入一些查询参数:

curl http://127.0.0.1:5984/pagination/_design/pagination/_view/by_unames?limit=11&startkey=%22101%22

这产生

{"total_rows":1000,"offset":4,"rows":[
{"id":"101","key":"101","value":"name_101"},
{"id":"102","key":"102","value":"name_102"},
{"id":"103","key":"103","value":"name_103"},
{"id":"104","key":"104","value":"name_104"},
{"id":"105","key":"105","value":"name_105"},
{"id":"106","key":"106","value":"name_106"},
{"id":"107","key":"107","value":"name_107"},
{"id":"108","key":"108","value":"name_108"},
{"id":"109","key":"109","value":"name_109"},
{"id":"11","key":"11","value":"name_11"},
{"id":"110","key":"110","value":"name_110"}
]}

请注意,尽管使用limit查询参数仅获取 11 个文档,total_rows但仍然是 1000,这是视图中文档的总数。

另请注意,startkey 参数可用于告诉 couchDB 从哪个文档开始计数。这可用于创建快速的“链表”样式分页,这是CouchDB 指南中推荐的方法。

对于您的具体情况,在您使用 python-couchdb 库的地方,一行如

 x = db.view('pagination/by_unames', limit=11, startkey="101")

可以使用。然后x.rows将有 11 行,从 id 为“101”的文档开始,x.total_rows将是 1000,这是视图中文档的总数。

于 2014-12-29T22:06:55.537 回答