ページネーション
□未翻訳
□翻訳中
□翻訳完了(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:
そしてこのように、"default.py"コントローラにデータベースを読むlist_itemアクションを作ります。
Now create an action list_items in the "default.py" controller that reads like this:
このコードは必要な個数より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選択あたりのアクションでページネーションができ、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.