tags:

views:

64

answers:

1

Only comments associated with the current page should be listed, so once again the query is modified to include the page ID. In this case, though, we also have to pass the pageid argument, which will in turn get passed to any h.url_for() calls in the paginator.

from http://pylonsbook.com/en/1.1/simplesite-tutorial-part-2.html

i cannot get this to work, the paginator does not pass things to the h.url_for, i followed the tutorial. i had to add pageid to the h.url_for in list.html. how do i solve?

part of the code:

        ${h.link_to(
            comment.id,
            h.url_for(
                controller=u'comment',
                action='view',
                id=unicode(comment.id)
            )
        )}

but it does not work properly until i put in

        ${h.link_to(
            comment.id,
            h.url_for(
                controller=u'comment',
                action='view',
                id=unicode(comment.id),
                pageid = c.page.id
            )
        )}

edit: the problem is that on the tutorial it said that the paginator will pass through with this code:

    c.paginator = paginate.Page(
        comments_q,
        page=int(request.params.get('page', 1)),
        items_per_page=10,
        pageid=c.page.id,
        controller='comment',
        action='list'
        )
    return render('/derived/comment/list.html')

but it does not happen unless I put it in manually

+1  A: 

You need to pass the pageid to the method url_for because the pageid is required by the route.

map.connect('/page/{pageid}/{controller}/{action}', requirements={'pageid':'\d+'})
map.connect('/page/{pageid}/{controller}/{action}/{id}', requirements={'pageid':'\d+', 'id':'\d+'})

The pageid is then processed in your comment controller in the before method

def __before__(self, action, pageid=None):
    page_q = meta.Session.query(model.Page)
    c.page = pageid and page_q.filter_by(id=int(pageid)).first() or None
    if c.page is None:
        abort(404)

Then, the c.page is set with the current page and comment can be link to this c.page.

sptremblay
thanks, i understand why it is required, but the problem is that it claims it would pass through - i will update question
Timmy