web.py可用的分页代码,bootstrap 分页样式哦

web.py 默认自己并没有分页的模块,所以这里自己写了一个

假设你的展示分页的class是news

先做 url 设置

 

"/news/(\d+)","home.app.news",

以上定义,根据自己站点结构调整哦

 

然后我们在 news.py中

把顶部的这些必须东西加上

 

#-*-coding:utf-8-*-import web
from web import storage
import model
from settings import render_home as render

j = storage()

 

 

 

接着定义 class news

 

class news:def GET(self, p='1'):
        p = int(p)
        j.article = model.get_all_article(p, cid='62')
        j.page = model.get_page(p, cid='62')return render('news',**j)

我的模板部分用的是jinja2,这里给一个参考配置,

 

 

#-*-coding:utf-8-*-import web
from web.contrib.template import render_jinja
from os.path import abspath, dirname, join
from jinja2 importEnvironment,FileSystemLoaderdef render_home(template_name,**context):
    extensions = context.pop('extensions',[])
    globals = context.pop('globals',{})
    app = context.pop('app','')

    jinja_env =Environment(
        loader=FileSystemLoader(join(dirname(__file__),'templates','home')),
        extensions=extensions,)
    jinja_env.globals.update(globals)

    tpl = app+template_name+'.html'#jinja_env.update_template_context(context)return jinja_env.get_template(tpl).render(context)

model中,有几个函数

 

 

先把分页的函数加进去

 

import web
page_size='15'def page(p=1, total=0):Page=[]if(total < page_size):
        page_count =1elif(total % page_size):
        page_count = total / page_size +1else:
        page_count = total / page_size

    if p > page_count:returnPageif page_count !=1:def link(l):Page.append('<a href="'+ str(l)+'">'+ str(l)+'</a>')if p !=1:Page.append('<a href="'+ str(p -1)+'">'+ u'<上一页'+'</a>')if p >11:for i in range(p -10, p):
                link(i)else:for i in range(1, p):
                link(i)Page.append('<a href="'+ str(p)+'"><b>'+ str(p)+'</b></a>')if p +10<= page_count:for i in range(p +1, p +11):
                link(i)else:passfor i in range(p +1, page_count +1):
                link(i)if p != page_count:Page.append('<a href="'+ str(p +1)+'">'+ u'下一页>'+'</a>')returnPage
接着
def get_all_article(p, cid):
    start =(p -1)* page_size
    offset = page_size
    article = db.select('article', where="cid=$cid", order='data desc,time desc,id desc',
                        limit="$start, $offset", vars=locals())return article#获取文章def get_page(p, cid):
    total = db.query("SELECT COUNT(*) AS total_article FROM article where cid=$cid", vars=locals())[0].total_article
    return page(p, total)#获取分页数据
以上,我们的代码部分就全部完成了
接着我们进入模板部分news.html
 
加入以下代码
<div class="info"><ul class="article">{%for a in article %}<li><span>{{ a.data }}</span><a href="/newsshow/{{ a.id }}">{{ a.title }}</a></li>{%if loop.index%5==0andnot loop.last %}<hr>{% endif %}{% endfor %}</ul><div class="pagination"><ul>{%for p in page %}<li>{{ p }}</li>{% endfor %}</ul></div></div>
好了,再加入我们的分页的样式
.pagination {
    margin:20px0;}.pagination ul {
    display: inline-block;
    list-style:none;*display: inline;/* IE7 inline-block hack */*zoom:1;
    margin-left:0;
    margin-bottom:0;-webkit-border-radius:4px;-moz-border-radius:4px;
    border-radius:4px;-webkit-box-shadow:01px2px rgba(0,0,0,0.05);-moz-box-shadow:01px2px rgba(0,0,0,0.05);
    box-shadow:01px2px rgba(0,0,0,0.05);}.pagination ul > li {
    display: inline;}.pagination ul > li > a,.pagination ul > li > span,.pagination #lastspan {
    float: left;
    padding:4px12px;
    line-height:20px;
    text-decoration: none;
    background-color:#ffffff;
    border:1px solid #dddddd;
    border-left-width:0;}.pagination ul > li > a:hover,.pagination ul > li > a:focus,.pagination ul >.active > a,.pagination ul >.active > span {
    background-color:#f5f5f5;}.pagination ul >.active > a,.pagination ul >.active > span {
    color:#999999;
    cursor: default;}.pagination ul >.disabled > span,.pagination ul >.disabled > a,.pagination ul >.disabled > a:hover,.pagination ul >.disabled > a:focus {
    color:#999999;
    background-color: transparent;
    cursor: default;}.pagination ul > li:first-child > a,.pagination ul > li:first-child > span {
    border-left-width:1px;-webkit-border-top-left-radius:4px;-moz-border-radius-topleft:4px;
    border-top-left-radius:4px;-webkit-border-bottom-left-radius:4px;-moz-border-radius-bottomleft:4px;
    border-bottom-left-radius:4px;}.pagination ul > li:last-child > a,.pagination ul > li:last-child > span,.pagination #lastspan {-webkit-border-top-right-radius:4px;-moz-border-radius-topright:4px;
    border-top-right-radius:4px;-webkit-border-bottom-right-radius:4px;-moz-border-radius-bottomright:4px;
    border-bottom-right-radius:4px;}.pagination-centered {
    text-align: center;}.pagination-right {
    text-align: right;}.pagination-large ul > li > a,.pagination-large ul > li > span,.pagination-large #lastspan{
    padding:11px19px;
    font-size:17.5px;}.pagination-large ul > li:first-child > a,.pagination-large ul > li:first-child > span {-webkit-border-top-left-radius:6px;-moz-border-radius-topleft:6px;
    border-top-left-radius:6px;-webkit-border-bottom-left-radius:6px;-moz-border-radius-bottomleft:6px;
    border-bottom-left-radius:6px;}.pagination-large ul > li:last-child > a,.pagination-large ul > li:last-child > span,.pagination-large #lastspan {-webkit-border-top-right-radius:6px;-moz-border-radius-topright:6px;
    border-top-right-radius:6px;-webkit-border-bottom-right-radius:6px;-moz-border-radius-bottomright:6px;
    border-bottom-right-radius:6px;}.pagination-mini ul > li:first-child > a,.pagination-small ul > li:first-child > a,.pagination-mini ul > li:first-child > span,.pagination-small ul > li:first-child > span {-webkit-border-top-left-radius:3px;-moz-border-radius-topleft:3px;
    border-top-left-radius:3px;-webkit-border-bottom-left-radius:3px;-moz-border-radius-bottomleft:3px;
    border-bottom-left-radius:3px;}.pagination-mini ul > li:last-child > a,.pagination-small ul > li:last-child > a,.pagination-mini ul > li:last-child > span,.pagination-small ul > li:last-child > span {-webkit-border-top-right-radius:3px;-moz-border-radius-topright:3px;
    border-top-right-radius:3px;-webkit-border-bottom-right-radius:3px;-moz-border-radius-bottomright:3px;
    border-bottom-right-radius:3px;}.pagination-small ul > li > a,.pagination-small ul > li > span {
    padding:2px10px;
    font-size:11.9px;}.pagination-mini ul > li > a,.pagination-mini ul > li > span {
    padding:06px;
    font-size:10.5px;}

大功告成!

截个图,

武汉长乐未央网络科技,版权所有,转载请注明.

转载于:https://www.cnblogs.com/canon4ever/p/3305729.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值