webpy实现分页功能的方法

分页,在做WEB开发的时候一定会遇到的,可是webpy却没有内置的分布类,只有自己动手写一个简单的分页类。本文,就以webpy(实际只是python的功能)为便,实现一个简单的分页类…

如何分页

以前,经常将分页与DB混在一起,比如以下的PHP代码:

<?php$page = get_current_page();$start = $page*$step;$article_list = $db->all('select * from `xxx` limit $start,$step;');$total = $db->get('select count(*) as `total` form `xxx`;');//...pagination...

这样的分页明显是不科学的。

分页应该注意的事项

  • 分页类不应与混淆在一起
  • 分页类不应与界面混淆在一起
  • 分页类应该独立,在任何情况下都可以使用

为了满足以上的三个条件,就设计了一个简单的分页类(只做示例,应用到项目需要优化代码和严格的检查)

 

分页类

分页类代码

 

class ProbbsPage:     def __init__(self, total, per = 10):          self.total = total          self.per = per          self.url = ''          self.page = 1               def set_url(self,url):          self.url = url          return self          def set_page(self,page):          self.page = int(page)          return self               def show(self):          if self.total%self.per == 0:               pages = self.total/self.per          else:               pages = self.total/self.per+1          if self.page < 6:               limit_s = 1          else:               limit_s = self.page                    if pages < (limit_s+10):               limit_e = pages          else:               limit_e = limit_s+10                    pagination = '<span>%s/%s pages </span>'%(self.page,pages)          for i in range(limit_s,limit_e+1):               if i == self.page:                    pagination += '<a class="cur" href="javascript:void(0);">%s</a>'%(i,)               else:                    pagination += '<a href="%s">%s</a>'%(self.url%i,i)                    return pagination

分页类调用示例

pagination =ProbbsPage(总页数,每页数)url = 'your_page?page=%s';page_html = pagination.set_url(url).set_page(page).show()print page_html#就可以显示出#<span>当前页/共几页</span>#<a>页页链接</a>

分类页可以优化的地方

set_url可以自动根据url进行提取,前提是使用“?页面参数=第几页”等常用的形成(像我使用专门的不期然形式就不可以了)
set_page同上,可以自动从url中上提取
如果做到以上的两步,就可以直接 ProbbsPage(总页数,每页数).show()就可以了,相对来说比较方便

文章来源:http://pjiaxu.com/python/48.html

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » webpy实现分页功能的方法

148 评论

  1. buy avodart without prescription order celecoxib order ondansetron for sale

  2. buy generic tadalafil 10mg voltaren drug voltaren without prescription

  3. provigil cost diamox order acetazolamide 250mg cost

  4. hytrin 5mg brand cheap arava azulfidine canada

  5. purchase molnupiravir for sale prevacid 30mg usa purchase lansoprazole generic

  6. furosemide for sale online lasix 100mg cost order hydroxychloroquine 200mg pill

  7. buying a term paper free casino casino online

  8. tadalafil 10mg uk buy cialis 20mg rx pharmacy online viagra

  9. best online casino real money roulette game tadalafil medication

  10. gambling meaning blackjack free purchase provigil online

  11. buy albuterol 100 mcg sale proventil generic buy cipro 500mg generic

  12. order dapoxetine without prescription avanafil price motilium canada

  13. After all, what a great site and informative posts, I will upload inbound link – bookmark this web site? Regards, Reader. Metropol Halı Karaca Halı Öztekin ve Selçuklu Halı Cami Halısı ve Cami Halıları Türkiye’nin En Büyük Cami Halısı Fabrikasıyız…

  14. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Metropol Halı Karaca Halı Öztekin ve Selçuklu Halı Cami Halısı ve Cami Halıları Türkiye’nin En Büyük Cami Halısı Fabrikasıyız…

  15. famotidine 40mg generic remeron price mirtazapine pills

  16. buy tadalafil online cheap amoxicillin pill amoxicillin 250mg usa

  17. purchase nexium capsules lasix generic buy generic furosemide

  18. provigil 200mg cost stromectol 12mg ca buy promethazine 25mg online cheap

  19. clomid pills lipitor uk prednisolone 10mg over the counter

  20. order doxycycline 200mg pill zovirax pills buy acyclovir 800mg online

  21. buy uroxatral 10mg online cheap diltiazem 180mg ca buy diltiazem without prescription

  22. citalopram or escitalopram for anxiety lexapro drug class para quГ© sirve escitalopram

  23. zoloft and breastfeeding autism zoloft and ocd average dose of zoloft

  24. order catapres 0.1 mg online buy meclizine spiriva over the counter

  25. order azithromycin for sale neurontin usa buy neurontin 800mg sale

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡