2010-04-07 41 views
2

只应列出与当前页面关联的注释,因此再一次将查询修改为包含页面标识。但是,在这种情况下,我们还必须传递pageid参数,该参数将反过来传递给paginator中的任何h.url_for()调用。标语标页符问题

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

我不能得到这个工作,分页程序的东西不会传递到h.url_for,我也跟着教程。我不得不将pageid添加到list.html中的h.url_for。我如何解决?代码

部分:

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

但直到我把

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

编辑它不能正常工作:问题是,在本教程它说,该分页程序将通过与此代码:

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') 

但它不会发生,除非我把它放在手动

+0

您使用的是最新的1.0测试版1吗? – Yaroslav 2010-04-07 20:23:20

+0

.97,正如 – Timmy 2010-04-07 22:07:40

回答

1

您需要将pageid传递给方法url_for,因为pageid是路由所必需的。

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

中的PageId然后在您的评论控制器处理之前方法

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) 

然后,c.page设置与当前页和注释可以链接到该C。页。

+0

书中所用,谢谢,我明白为什么它是必需的,但问题是,它声称它会通过 - 我会更新问题 – Timmy 2010-04-08 14:48:22