A list/iterator of items representing one page in a larger collection.
An instance of the “Page” class is created from a collection of things. The instance works as an iterator running from the first item to the last item on the given page. The collection can be:
A “Page” instance maintains pagination logic associated with each page, where it begins, what the first/last item on the page is, etc. The pager() method creates a link list allowing the user to go to other pages.
WARNING: Unless you pass in an item_count, a count will be performed on the collection every time a Page instance is created. If using an ORM, it’s advised to pass in the number of items in the collection if that number is known.
Instance attributes:
Return string with links to other pages (e.g. “1 2 [3] 4 5 6 7”).
Format string that defines how the pager is rendered. The string can contain the following $-tokens that are substituted by the string.Template module:
To render a range of pages the token ‘~3~’ can be used. The number sets the radius of pages around the current page. Example for a range with radius 3:
‘1 .. 5 6 7 [8] 9 10 11 .. 500’
Default: ‘~2~’
String to be displayed as the text for the %(link_first)s link above.
Default: ‘<<’
String to be displayed as the text for the %(link_last)s link above.
Default: ‘>>’
String to be displayed as the text for the %(link_previous)s link above.
Default: ‘<’
String to be displayed as the text for the %(link_next)s link above.
Default: ‘>’
String that is used to seperate page links/numbers in the above range of pages.
Default: ‘ ‘
When using AJAX/AJAH to do partial updates of the page area the application has to know whether a partial update (only the area to be replaced) or a full update (reloading the whole page) is required. So this parameter is the name of the URL parameter that gets set to 1 if the ‘onclick’ parameter is used. So if the user requests a new page through a Javascript action (onclick) then this parameter gets set and the application is supposed to return a partial content. And without Javascript this parameter is not set. The application thus has to check for the existance of this parameter to determine whether only a partial or a full page needs to be returned. See also the examples in this modules docstring.
Default: ‘partial’
if True the navigator will be shown even if there is only one page
Default: False
A dictionary of attributes that get added to A-HREF links pointing to other pages. Can be used to define a CSS style or class to customize the look of links.
Example: { ‘style’:’border: 1px solid green’ }
Default: { ‘class’:’pager_link’ }
A dictionary of attributes that get added to the current page number in the pager (which is obviously not a link). If this dictionary is not empty then the elements will be wrapped in a SPAN tag with the given attributes.
Example: { ‘style’:’border: 3px solid blue’ }
Default: { ‘class’:’pager_curpage’ }
A dictionary of attributes that get added to the ‘..’ string in the pager (which is obviously not a link). If this dictionary is not empty then the elements will be wrapped in a SPAN tag with the given attributes.
Example: { ‘style’:’color: #808080’ }
Default: { ‘class’:’pager_dotdot’ }
This paramter is a string containing optional Javascript code that will used as the ‘onclick’ action of each pager link. Use ‘%s’ in your string where the URL linking to the desired page will be inserted. This can be used to enhance your pager with AJAX actions loading another page into a DOM object. Note that the URL to the destination page contains a ‘partial_param’ parameter so that you can distinguish between AJAX requests (just refreshing the paginated area of your page) and full requests (loading the whole new page).
Additional keyword arguments are used as arguments in the links. Otherwise the link will be created with url_for() which points to the page you are currently displaying.
The unit tests are often educational ...
""""Test webhelpers.paginate package."""
from routes import Mapper
from webhelpers.paginate import Page
def test_empty_list():
"""Test whether an empty list is handled correctly."""
items = []
page = Page(items, page=0)
assert page.page == 0
assert page.first_item is None
assert page.last_item is None
assert page.first_page is None
assert page.last_page is None
assert page.previous_page is None
assert page.next_page is None
assert page.items_per_page == 20
assert page.item_count == 0
assert page.page_count == 0
assert page.pager() == ''
assert page.pager(show_if_single_page=True) == ''
def test_one_page():
"""Test that we fit 10 items on a single 10-item page."""
items = range(10)
page = Page(items, page=0, items_per_page=10)
assert page.page == 1
assert page.first_item == 1
assert page.last_item == 10
assert page.first_page == 1
assert page.last_page == 1
assert page.previous_page is None
assert page.next_page is None
assert page.items_per_page == 10
assert page.item_count == 10
assert page.page_count == 1
assert page.pager() == ''
assert page.pager(show_if_single_page=True) == \
'<span class="pager_curpage">1</span>'
def test_many_pages():
"""Test that 100 items fit on seven 15-item pages."""
# Create routes mapper so that webhelper can create URLs
# using webhelpers.url_for()
mapper = Mapper()
mapper.connect(':controller')
items = range(100)
page = Page(items, page=0, items_per_page=15)
assert page.page == 1
assert page.first_item == 1
assert page.last_item == 15
assert page.first_page == 1
assert page.last_page == 7
assert page.previous_page is None
assert page.next_page == 2
assert page.items_per_page == 15
assert page.item_count == 100
assert page.page_count == 7
print page.pager()
assert page.pager() == \
'<span class="pager_curpage">1</span> ' + \
'<a class="pager_link" href="/content?page=2">2</a> ' + \
'<a class="pager_link" href="/content?page=3">3</a> ' + \
'<span class="pager_dotdot">..</span> ' + \
'<a class="pager_link" href="/content?page=7">7</a>'
assert page.pager(separator='_') == \
'<span class="pager_curpage">1</span>_' + \
'<a class="pager_link" href="/content?page=2">2</a>_' + \
'<a class="pager_link" href="/content?page=3">3</a>_' + \
'<span class="pager_dotdot">..</span>_' + \
'<a class="pager_link" href="/content?page=7">7</a>'
assert page.pager(page_param='xy') == \
'<span class="pager_curpage">1</span> ' + \
'<a class="pager_link" href="/content?xy=2">2</a> ' + \
'<a class="pager_link" href="/content?xy=3">3</a> ' + \
'<span class="pager_dotdot">..</span> ' + \
'<a class="pager_link" href="/content?xy=7">7</a>'
assert page.pager(
link_attr={'style':'s1'},
curpage_attr={'style':'s2'},
dotdot_attr={'style':'s3'}) == \
'<span style="s2">1</span> ' + \
'<a href="/content?page=2" style="s1">2</a> ' + \
'<a href="/content?page=3" style="s1">3</a> ' + \
'<span style="s3">..</span> ' + \
'<a href="/content?page=7" style="s1">7</a>'
Powered by Pylons - Contact Administrators
Comments (4)
Hello,
I tried to use the paginate.Page class with an SQLAlchemy select object.
This works quite well, but what if someone wants to use BindParams in the
select object.
In SQLAlchemy you usualy have to call Session.execute(select, binds) to
support the necessary bind values for each bind parameter.
binds is here a dict like: {bindparam1, ‘bindparam2’:‘some_value’}
From a database perspective bind params are necessary for good performance, so
that the database don’t have to parse the whole SQL again and again.
Unfortunately there is not support in the Page class for bind params.
It would be realy great if this could be implemented, as this is critical
for good DB-performance.
———————-
Example:
tf = some_table
q = tf.select()
q = q.where(tf.c.departure_date >= bindparam(‘departure_date’))
paginator = paginate.Page(q, page=1, items_per_page = 2)
This will not work, because there is no way to set the value of the bindparam. :-(
———————-
best regards
Oliver
Just prepare a Select or Query object containing the correct result set and pass it to paginate.Page(). The paginate module will just split up the result set and display it page-wise. Bear with me but I don’t see a reason why bind parameters are useful here.
Using this example, straight from the pylons book chapter 8, I get a url_for error..
Controller… seems to work fine
def list(self):
records = meta.Session.query(model.Page)
c.paginator = paginate.Page(
records,
page=int(request.params.get(‘page’, 1)),
items_per_page = 10,
)
return render(’/derived/page/list.html’)
The lines that break the program are in the template. Any reference to c.paginator.pager() with any options in it, or not in it, the error is the same. Everything else involving c.paginator seems to work just fine.
${ c.paginator.pager()}
## With or without any options
GenerationException: url_for could not generate URL. Called with args: () {‘page’: 2}
Any thoughts as to what this could be?
I have the same problem with Chapter 8 of the Pylons book. I posted a trackback. If you have the solution, I’m interested ;-)
http://pylonshq.com/tracebacks/af4b2087ed15e60e70726c05e20c5f83
Suggest an addition to the docs, or report errors. Note that we will delete documentation fixes as they're applied.
You must login before you can comment.