ページネーション

□未翻訳

□翻訳中

□翻訳完了(Yota Ichino)

■レビュー(Omi Chiba)

ページネーション

このレシピは、ページネーション場合にデータベースのアクセス最小化することに対して役立つ技術です。例えば、データベースから行のリストを表示する必要があるが複数のページにまたがる場合です。

This recipe is a useful trick to minimize database access in case of pagination, e.g., when you need to display a list of rows from a database but you want to distribute the rows over multiple pages.

まず、データベースに1000個の素数を1番目から格納するprimesアプリケーションを製作することから始めましょう。

Start by creating a primes application that stores the first 1000 prime numbers in a database.

db.pyモデル:

Here is the model db.py:

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

db = DAL('sqlite://primes.db')

db.define_table('prime',Field('value','integer'))

def isprime(p):

for i in range(2,p):

if p%i==0: return False

return True

if len(db().select(db.prime.id))==0:

p=2

for i in range(1000):

while not isprime(p): p+=1

db.prime.insert(value=p)

p+=1

そしてこのように、"default.py"コントローラにデータベースを読むlist_itemアクションを作ります。

Now create an action list_items in the "default.py" controller that reads like this:

1.

2.

3.

4.

5.

6.

7.

def list_items():

if len(request.args): page=int(request.args[0])

else: page=0

items_per_page=20

limitby=(page*items_per_page,(page+1)*items_per_page+1)

rows=db().select(db.prime.ALL,limitby=limitby)

return dict(rows=rows,page=page,items_per_page=items_per_page)

このコードは必要な個数より1つ多いアイテムを選択することに注意してください(20+1)。範囲外の要素は次のページがあるかどうかを示します。

Notice that this code selects one more item than is needed, 20+1. The extra element tells the view whether there is a next page.

"default/list_items.html"ビュー:

Here is the "default/list_items.html" view:

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

{{extend 'layout.html'}}

{{for i,row in enumerate(rows):}}

{{if i==items_per_page: break}}

{{=row.value}}<br />

{{pass}}

{{if page:}}

<a href="{{=URL(args=[page-1])}}">previous</a>

{{pass}}

{{if len(rows)>items_per_page:}}

<a href="{{=URL(args=[page+1])}}">next</a>

{{pass}}

この方法で1選択あたりのアクションでページネーションができ、1つの選択だけで必要な行だけを選んでくれます。

In this way we have obtained pagination with one single select per action, and that one select only selects one row more then we need.