效果如下(展示用户所有发布得新闻,新闻状态,分页
1、前端代码
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/news/css/reset.css"> 7 <link rel="stylesheet" type="text/css" href="../../static/news/css/jquery.pagination.css"> 8 <link rel="stylesheet" type="text/css" href="../../static/news/css/main.css"> 9 <script type="text/javascript" src="../../static/news/js/jquery-1.12.4.min.js"></script> 10 <script type="text/javascript" src="../../static/news/js/jquery.pagination.min.js"></script> 11 </head> 12 <body class="inframe_body"> 13 <div class="news_list"> 14 <h3>新闻列表</h3> 15 <ul class="article_list"> 16 {% for news in context.news_list %} 17 <li> 18 {% if news.status == 0 %} 19 {# 审核通过 #} 20 <a href="#">{{ news.title }}</a><em class="pass">已通过</em><span>{{ news.create_time }}</span> 21 {% elif news.status == 1 %} 22 {# 审核中 #} 23 <a href="javascript:;">{{ news.title }}</a><em class="review">审核中</em><span>{{ news.create_time }}</span> 24 {% else %} 25 {# 审核不通过 #} 26 <a href="javascript:;">{{ news.title }}</a><em class="nopass">未通过</em><span>{{ news.create_time }}</span> 27 <b>未通过原因:{{ news.reason }}</b> 28 {% endif %} 29 </li> 30 {% endfor %} 31 </ul> 32 <div id="pagination" class="page"></div> 33 <script> 34 // 调用父页面的方法改变菜单指示 35 // window.parent.fnChangeMenu(2); 36 37 $(function(){ 38 $("#pagination").pagination({ 39 currentPage: {{ context.current_page }}, 40 totalPage: {{ context.total_page }}, 41 callback: function(current) { 42 window.location.href = '/user/user_news_list?p=' + current; 43 } 44 }); 45 }); 46 </script> 47 48 </div> 49 </body> 50 </html>
2、后台代码
1 @user_blue.route("/user_news_list") 2 @user_login_data 3 def user_news_list(): 4 user = g.user 5 if not user: 6 return redirect(url_for('index.index')) 7 page = request.args.get("p",1) 8 try: 9 # 将接收来的参数强转为int类型 10 page = int(page) 11 except Exception as e: 12 current_app.logger.error(e) 13 # 二次保障设置默认值为1 14 page = 1 15 #给变量赋值默认值 16 news_li = [] 17 current_page = 1 18 total_page = 1 19 try: 20 #数据库查询,分页器 21 paginate = News.query.filter(News.user_id == user.id).paginate(page = page, per_page=2) 22 news_li = paginate.items 23 current_page = paginate.page 24 total_page = paginate.pages 25 except Exception as e: 26 current_app.logger.error(e) 27 return jsonify(errno=RET.DATAERR, errmsg="查询失败") 28 29 #传递上下文 30 context = { 31 "news_list":news_li, 32 "current_page":current_page, 33 "total_page":total_page, 34 } 35 #渲染页面 36 return render_template("news/user_news_list.html",context = context)