1. 程式人生 > 實用技巧 >1.3使用者列表 and 新聞列表

1.3使用者列表 and 新聞列表

1、實現效果:(新聞列表和這個基本一樣,就不單獨開部落格了

2、老三步:

  1.1將統計頁面放入templates/admin中

  

  1.2、修改頁面的呼叫路徑

  

  1.3後臺檢視函式

 1 @admin_blue.route('/user_list')
 2 def user_list():
 3     #接收前端傳來得p
 4     page = request.args.get("p")
 5     try:
 6         # 將接收來的引數強轉為int型別
 7         page = int(page)
 8     except Exception as e:
9 current_app.logger.error(e) 10 # 二次保障設定預設值為1 11 page = 1 12 try: 13 # 獲取使用者收藏的新聞列表並使用分頁器接收 14 paginate = User.query.filter(User.is_admin == False).paginate(page=page, per_page=constants.ADMIN_USER_PAGE_MAX_COUNT) 15 #獲取總資料 16 userList = paginate.items
17 #頁面總數 18 total_page = paginate.pages 19 #當前頁面數 20 current_page = page 21 except Exception as e: 22 current_app.logger.error(e) 23 return jsonify(errno=RET.DATAERR, errmsg="查詢失敗") 24 #傳遞上下文 25 context = { 26 "userList":userList, 27
"current_page":current_page, 28 "total_page":total_page 29 } 30 return render_template("admin/user_list.html",context=context)

 3、前端程式碼

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>新經資訊後臺管理</title>
 6     <link rel="stylesheet" type="text/css" href="../../static/admin/css/reset.css">
 7     <link rel="stylesheet" type="text/css" href="../../static/admin/css/main.css">
 8     <link rel="stylesheet" href="../../static/admin/css/jquery.pagination.css">
 9     <script type="text/javascript" src="../../static/admin/js/jquery-1.12.4.min.js"></script>
10     <script type="text/javascript" src="../../static/admin/js/jquery.pagination.min.js"></script>
11 </head>
12 <body>
13     <div class="breadcrub">
14             當前位置:使用者管理>使用者列表
15         </div>
16         <div class="pannel">
17             <table class="common_table">
18                 <tr>
19                     <th>使用者名稱</th>
20                     <th>電話</th>
21                     <th>註冊時間</th>
22                     <th>上次登入時間</th>
23                 </tr>
24 
25                 {% for user in context.userList %}
26                     <tr>
27                         <td>{{ user.nick_name }}</td>
28                         <td>{{ user.mobile }}</td>
29                         <td>{{ user.create_time }}</td>
30                         <td>{{ user.last_login }}</td>
31                     </tr>
32                 {% endfor %}
33 
34             </table>
35         </div>
36 
37         <div class="box">
38             <div id="pagination" class="page"></div>
39         </div>
40 
41         <script>
42             $(function() {
43                 $("#pagination").pagination({
44                     currentPage: {{ context.current_page }},
45                     totalPage: {{ context.total_page }},
46                     callback: function(current) {
47                         window.location.href = '/admin/user_list?p=' + current;
48                     }
49                 });
50             });
51         </script>
52 </body>
53 </html>