您好,登錄后才能下訂單哦!
1、views 如下示例代碼:
@admin.route("/account/list/<int:page>/",?methods=["GET"]) @admin_login_req def?account_list(page=None): ????if?page?is?None: ????????page?=?1 ????page_data?=?Account.query.order_by( ????????Account.name.desc() ????).paginate(page=page,?per_page=10) ????return?render_template('admin/account_list.html',?page_data=page_data)
2、模板 account_list.html
{%?extends?'admin/base.html'?%} {%?import?"admin/ui/admin_page.html"?as?pg?%} {%?block?css?%} {%?endblock?%} {%?block?content?%} <div?class="panel?panel-default"> ????<div?class="panel-heading"> ????????查詢條件 ????</div> ????<div?class='panel-body'> ????????<div?class="row"> ????????????<form?role="form"> ????????????????<div?class="col-sm-6?col-md-6?col-lg-4"> ????????????????????<div?class="form-group"> ????????????????????????<div?class="input-group"> ????????????????????????????<div?class="input-group-addon">查找</div> ????????????????????????????<input?class="form-control"?type="email"> ????????????????????????</div> ????????????????????</div> ????????????????</div> ????????????????<div?class="col-lg-12"> ????????????????????<hr?class="mt5?mb15"> ????????????????????<button?type="submit"?class="king-btn?king-info">查詢</button> ????????????????????<a?class="king-btn?king-success"?href="{{?url_for('admin.account_add')?}}">添加賬號</a> ????????????????</div> ????????????</form> ????????</div> ????</div> </div> <!----> <div?class="panel?panel-default"> ????<div?class="panel-heading"> ????????帶邊框的表格 ????</div> ????{%?for?msg?in?get_flashed_messages(category_filter=["ok"])?%} ????<div?class="king-notice2?king-notice-success?notice-borders"> ????????<i?class="fa?fa-check-circle"></i> ????????<div?class="notice-text"> ????????????<p>{{?msg?}}</p> ????????</div> ????</div> ????{%?endfor?%} ????<div?class='panel-body'> ????????<div?class="table-responsive"> ????????????<table?class="table?table-bordered?table-hover?table-striped"> ????????????????<thead> ????????????????<tr> ????????????????????<th>名稱</th> ????????????????????<th>網站/IP</th> ????????????????????<th>用戶名</th> ????????????????????<th>密碼</th> ????????????????????<th>備注</th> ????????????????????<th>操作</th> ????????????????</tr> ????????????????</thead> ????????????????<tbody> ????????????????{%?for?v?in?page_data.items?%} ????????????????<tr> ????????????????????<td>{{?v.name?}}</td> ????????????????????<td>{{?v.website?}}</td> ????????????????????<td>{{?v.username?}}</td> ????????????????????<td>{{?v.pwd?}}</td> ????????????????????<td>{{?v.comment?}}</td> ????????????????????<td> ????????????????????????<button?type="submit"?class="king-btn?king-warning">編輯</button> ????????????????????????<a?href="{{?url_for('admin.account_del',?id=v.id)?}}"?type="submit" ???????????????????????????class="king-btn?king-danger">刪除</a> ????????????????????</td> ????????????????</tr> ????????????????{%?endfor?%} ????????????????<tfoot> ????????????????<tr> ????????????????????<td?colspan="6"> ????????????????????????<div?class="pagination-info?pull-left">共有{{?page_data.total?}}條,每頁顯示:{{?page_data.per_page?}}條 ????????????????????????</div> ????????????????????????<div?class="pull-right?king-page-box"> ????????????????????????????{{?pg.page(page_data,?'admin.account_list')?}} ????????????????????????</div> ????????????????????</td> ????????????????</tr> ????????????????</tfoot> ????????????????</tbody> ????????????</table> ????????</div> ????</div> </div> </div> {%?endblock?%} {%?block?js?%} {%?endblock?%}
3、admin_page.html? 為分頁的模板
{%?macro?page(data,?url)?-%} {%?if?data?%} <ul?class="pagination?pagination-small?pull-right"> ????<li><a?href="{{?url_for(url,?page=1)?}}">首頁</a></li> ????{%?if?data.has_prev?%} ????<li?class="active"><a?href="{{?url_for(url,?page=data.prev_num)?}}">上一頁</a></li> ????{%?else?%} ????<li?class="disabled"><a?href="#">上一頁</a></li> ????{%?endif?%} ????{%?for?v?in?data.iter_pages()?%} ????????{%?if?v?==?data.page?%} ????????<li?class="active"><a>{{?v?}}</a></li> ????????{%?else?%} ????????<li><a?href="{{?url_for(url,?page=v)?}}">{{?v?}}</a></li> ????????{%?endif?%} ????{%?endfor?%} ????{%?if?data.has_next?%} ????????<li?class="active"><a?href="{{?url_for(url,?page=data.next_num)?}}">下一頁</a></li> ????{%?else?%} ????????<li?class="disabled"><a?href="#">下一頁</a></li> ????{%?endif?%} ????<li><a?href="{{?url_for(url,?page=data.pages)?}}">尾頁</a></li> </ul> {%?endif?%} {%-?endmacro?%}
flask-sqlalchemy 的分頁參考文檔 http://www.pythondoc.com/flask-sqlalchemy/api.html#id1
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。