- 帖子
- 4
- 精华
- 0
- 积分
- 48
- 阅读权限
- 10
- 注册时间
- 2016-7-3
- 最后登录
- 2016-10-10
|
关于webpy分页的问题,不是太清楚怎么实现,然后去网上找了一段代码。修改后可以使用,但是对应的地址是 http://127.0.0.1:8080/?page=1,
想知道怎么改成 http://127.0.0.1:8080/list/?page=1
程序中对应page并无url的设置呢
class list:
def GET(self):
i = web.input(page='1')
page = int(i.page)
page_posts, page_count = model.Post().list(page)
return render.list(page_posts, page_count, page)
----------------------
def list(self, page):
'''获取第page页的所有文章'''
per_page = 10
# 获取从offset开始共per_page个post
offset = (page - 1) * per_page
posts = db.query('''SELECT posts.id, title, posts.time, user_id, user_info.user_code AS username
FROM posts JOIN user_info
ON posts.user_id = user_info.id
ORDER BY posts.id DESC
LIMIT %d OFFSET %d''' % (per_page, offset))
page_posts = []
for p in posts:
comment = Comment(p.id)
last = comment.last()
last_time = last.time if last else p.time
page_posts.append({'id': p.id, 'title': p.title, 'userid': p.user_id, 'username': p.username, 'comment_count': comment.count(), 'last_time': last_time})
# 计算总页数
post_count = self.count()
page_count = post_count / per_page
if post_count % per_page > 0:
page_count += 1
return (page_posts, page_count)
|
|