Roman,

Here is the solution I am currently using.  First, I have a Paginator
class like:

class Paginator(object):
    def __init__(self, results, per_page, page=0, total=None):
        if not total:
            total = results.count()
        if total < per_page*page:
            raise NotFound
        self.pages = int(ceil(total / float(per_page)))
        self.total, self.results = total, results
        self.per_page, self.page = per_page, page
        self.has_previous = self.page > 0
        self.has_next = self.pages-1 > self.page

    def items(self):
        offset = self.per_page * self.page
        end = offset + self.per_page
        return list(self.results[offset:end])

    def page_range(self, range=10):
        if self.page-range < 1:
            start = 1
        else:
            start = self.page-range
        if page+range > self.pages:
            end = self.pages
        else:
            end = self.page+range
        return range(start-1, end)

(Note that this needs to be hacked a bit for efficiency - it should
probably use properties and lazy-load total from results.count())


I use it like this in a controller (say to paginate a list of blog
comments):

posts = Post.select(Post.q.blogID == topic.id, orderBy="id")
paginator = Paginator(posts, per_page=25, page=page)
return dict(paginator=paginator, blog=blog, posts=paginator.items())


Now in your kid template you have paginator.has_next,
paginator.has_previous, etc.  The paginator.page_range function will let
you do lists of available pages like gooooooogle does on large search
results :)

I stole this idea from Django - I have actually ported/stolen a lot of
things from Django (some generic views, get_object_or_404, timesince,
etc) to TurboGears.



Sean Cazzell



On Thu, 2005-11-10 at 12:12 -0800, Roman wrote:
> 
> Hi
> 
> A piece of code often needed is that which lets you present a large
> database result set that spans across several HTML pages (that the user
> can navigate). As in Gooooooogle.
> 
> Does TG (or SQLObject) somehow have native support for this?
> (or will it in the future?)
> 
> If not, where should I start when using TG, SQLObject and
> MySQLPagedResultSet?
> 
> Cheers,
> Roman
> 

Reply via email to