文章目录
显示
账号管理有3个部分
- 列表支持
- 详情
- 编辑(添加删除)
列表查询展示:
account/index.html
成功展示:
分页功能实现
Helper.py添加统一分页方法:
'''
自定义分页类
'''
def iPagination( params ):
import math
ret = {
"is_prev":1,
"is_next":1,
"from" :0 ,
"end":0,
"current":0,
"total_pages":0,
"page_size" : 0,
"total" : 0,
"url":params['url']
}
total = int( params['total'] )
page_size = int( params['page_size'] )
page = int( params['page'] )
display = int( params['display'] )
total_pages = int( math.ceil( total / page_size ) )
total_pages = total_pages if total_pages > 0 else 1
if page <= 1:
ret['is_prev'] = 0
if page >= total_pages:
ret['is_next'] = 0
semi = int( math.ceil( display / 2 ) )
if page - semi > 0 :
ret['from'] = page - semi
else:
ret['from'] = 1
if page + semi <= total_pages :
ret['end'] = page + semi
else:
ret['end'] = total_pages
ret['current'] = page
ret['total_pages'] = total_pages
ret['page_size'] = page_size
ret['total'] = total
ret['range'] = range( ret['from'],ret['end'] + 1 )
return ret
base_setting.py添加总页数
Account.py传递分页数据给前端
# -*- coding: utf-8 -*-
from flask import Blueprint,request
from common.libs.Helper import ops_render,iPagination
from common.models.User import User
from application import app,db
route_account = Blueprint( 'account_page',__name__ )
@route_account.route( "/index" )
def index():
resp_data = {}
req = request.values
page = int(req['page'])if ('page' in req and req['page']) else 1
query = User.query
page_params = {
'total':query.count(),
'page_size':app.config('PAGE_SIZE'),
'page' :page,
'display':app.config['PAGE_DISPLAY'],
'url':'/account/index'
}
pages = iPagination(page_params)
offset = (page-1)*app.config('PAGE_SIZE')
limit = app.config('PAGE_SIZE')*page
list = query.order_by(User.uid.desc()).all()[offset:limit]
resp_data[ 'list'] = list
resp_data[ 'pages'] = pages
return ops_render( "account/index.html" , resp_data)
templates/common放置pagenation.html
<div class="row">
<div class="col-lg-12">
<span class="pagination_count" style="line-height: 40px;">共{{ pages.total }}条记录 | 每页{{pages.page_size}}条</span>
<ul class="pagination pagination-lg pull-right" style="margin: 0 0 ;">
{% if pages.is_prev == 1 %}
<li>
<a href="{{ pages.url }}&p=1" ><span>首页</span></a>
</li>
{% endif %}
{% for idx in pages.range %}
{% if idx == pages.current %}
<li class="active"><a href="javascript:void(0);">{{ idx }}</a></li>
{% else %}
<li><a href="{{ pages.url }}&p={{idx}}">{{ idx }}</a></li>
{% endif %}
{% endfor %}
{% if pages.is_next == 1 %}
<li>
<a href="{{ pages.url }}&p={{ pages.total_pages }}" ><span>尾页</span></a>
</li>
{% endif %}
</ul>
</div>
</div>
详情页面的动态支持
from common.libs.UrlManager import UrlManager
@route_account.route( "/info" )
def info():
resp_data = {}
req = request.args
uid = int(req.get('id', 0))
reback_url = UrlManager.buildUrl("/account/index")
if uid < 1:
return redirect(reback_url)
info = User.query.filter_by(uid=uid).first()
if not info:
return redirect(reback_url)
resp_data['info'] = info
return ops_render( "account/info.html" , resp_data)
info.html修改模板:
添加账号页面的添加
info.html添加编辑跳转:
Account.py添加post支持
@route_account.route( "/set" ,methods = ["GET","POST"])
def set():
if request.method == "GET":
return ops_render( "account/set.html" )