Created
September 1, 2013 07:04
-
-
Save xingkaixin/6402792 to your computer and use it in GitHub Desktop.
From http://www.cnblogs.com/rails3/
web.py 分页
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import web | |
import MySQLdb | |
db = web.database(dbn='mysql',user='root',pw='root',db='python_test') #连接数据库 | |
render = web.template.render('templates/') #模板路径 | |
urls = ( #url设置 | |
'/','index', | |
'/page/(\d+)','index' | |
) | |
class index: | |
def GET(self,page=1): #分页函数 | |
page = int(page) | |
perpage = 10 | |
offset = (page-1)*perpage | |
posts = db.select('ykgame',order="date DESC",offset=offset, | |
limit=perpage) | |
postcount = db.query('select count(*) as count from ykgame')[0] | |
pages = postcount.count / perpage | |
if postcount.count % perpage > 0: | |
pages += 1 | |
if page > pages: | |
raise web.seeother('/') | |
else: | |
return render.index(posts=posts,pages=pages) | |
if __name__ == "__main__": | |
app = web.application(urls,globals()) | |
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$def with (posts,pages) | |
<ul> | |
$for post in posts: | |
<li id= "t$post.id"><a href="$post.href">$post.title</a> $post.date</li> | |
</ul> | |
$for page in range(1,pages+1): | |
<a href="/page/$page">$page</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment