主页 > 编程 > python >

flask+pymysql分页代码 flask不用分页插件怎么分页

2019-01-18 21:45 阅读:85 来源:智宇SEO自媒体

 

  没有用flask分页插件,用最原始的方法,撸起袖子干。话不多说直接贴代码吧

flask+pymysql分页代码 flask不用分页插件怎么分页

 

  控制器代码  


				
@app.route('/',methods=['GET','POST'])
def hello_world():
    nowPage = request.args.get('page', type=int, default=1)
    # 每页显示数据条数
    pageSize = 2
    # 总留言数
    totaNums = getMsgCount()
    totaNum=int(totaNums[0][0])
    # 总页数
    totaPageCount = math.ceil(totaNum / pageSize)
    #生成页数
    my_list = list(range(totaPageCount+1))
    pageList=my_list[1:]
    if not nowPage:
        nowPage=1
    #上一页
    if (nowPage-1)<=0:
        prev=1
    else:
        prev=nowPage-1

    #下一页
    if ((nowPage+1)>=totaPageCount):
        nextpg=totaPageCount
    else:
        nextpg=nowPage+1

    # 偏移量
    offset = (nowPage - 1) * pageSize;
    msgdata = showMsg(offset,pageSize)
    if msgdata:
        return render_template('index.html',prev=prev,nextpg=nextpg, msgdata=msgdata,totaNum=totaNum,totaPageCount=totaPageCount,pageList=pageList)
    else:
        point = "暂时没有留言信息"
        return render_template('index.html', point=point)



 模型代码

#显示留言
def showMsg(a,b):
    conn = mysqlConn()
    cur = conn.cursor()
    sql = "SELECT yulyb_messgae.id,title,yulyb_msgtype.name, yulyb_messgae.name,content,date FROM yulyb_messgae INNER JOIN yulyb_msgtype ON (yulyb_msgtype.id = yulyb_messgae.typeid) order by yulyb_messgae.id desc limit %d,%d" % (a,b)
    try:
        cur.execute(sql)
        result = cur.fetchall()
        if (len(result)) == 0:
            return False
        else:
            return result
    except Exception as e:
        raise e
    finally:
        cur.close()
        conn.close()
#获取留言数量
def getMsgCount():
    conn = mysqlConn()
    cur = conn.cursor()
    sql="select count(*) from yulyb_messgae"
    try:
        cur.execute(sql)
        result = cur.fetchall()
        if (len(result)) == 0:
            return False
        else:
            return   result
    except Exception as e:
        raise e
    finally:
        cur.close()
        conn.close()

  视图代码


   <div class="pagination">
   <a href="{{url_for('index')}}?page={{ prev }}">上一页</a>
       {%  for p in pageList %}
<a href="{{url_for('index')}}?page={{ p }}">{{ p }}</a>
       {% endfor %}
    <a href="{{url_for('index')}}?page={{ nextpg }}">下一页</a>
      {{ totaNum }}条留言&nbsp;&nbsp;{{ totaPageCount }}   </div>