BBS论坛(三十二)
论坛 三十二 BBS
2023-09-11 14:19:33 时间
32.帖子排序功能完成
(1)front_index.html
<ul class="post-group-head"> {% if current_sort==1 %} <li class="active"><a href="{{ url_for('front.index',bd=current_board) }}">最新</a></li> {% else %} <li><a href="{{ url_for('front.index',bd=current_board) }}">最新</a></li> {% endif %} {% if current_sort==2 %} <li class="active"><a href="{{ url_for('front.index',st=2,bd=current_board) }}">精华帖子</a></li> {% else %} <li><a href="{{ url_for('front.index',st=2,bd=current_board) }}">精华帖子</a></li> {% endif %} {% if current_sort==3 %} <li class="active"><a href="{{ url_for('front.index',st=3,bd=current_board) }}">点赞最多</a></li> {% else %} <li><a href="{{ url_for('front.index',st=3,bd=current_board) }}">点赞最多</a></li> {% endif %} {% if current_sort==4 %} <li class="active"><a href="{{ url_for('front.index',st=4,bd=current_board) }}">评论最多</a></li> {% else %} <li><a href="{{ url_for('front.index',st=4,bd=current_board) }}">评论最多</a></li> {% endif %} </ul> <ul class="post-list-group"> {% for post in posts %} <li> <div class="author-avatar-group"> <img src="{{ post.author.avatar or url_for('static',filename='common/images/logo.jpg') }}" alt=""> </div> <div class="post-info-group"> <p class="post-title"><a href="{{ url_for('front.post_detail',post_id=post.id) }}">{{ post.title }}</a> {% if post.highlight %} <span class="label label-danger">精华帖</span> {% endif %} </p> <p class="post-info"> <span>作者:{{ post.author.username }}</span> <span>发表时间:{{ post.create_time }}</span> <span>评论:0</span> <span>阅读数 :0</span> </p> </div> </li> {% endfor %} </ul>
(2)front/views.py
from sqlalchemy import func @bp.route('/') def index(): board_id=request.args.get('bd',type=int,default=None) page = request.args.get(get_page_parameter(), type=int, default=1) sort=request.args.get('st',type=int,default=1) banners = BannerModel.query.order_by(BannerModel.priority.desc()).limit(4) boards = BoardModel.query.all() start = (page - 1) * config.PER_PAGE end = start + config.PER_PAGE posts=None total=0 query_obj=None if sort==1: query_obj=PostModel.query.order_by(PostModel.create_time.desc()) elif sort==2: query_obj=db.session.query(PostModel).outerjoin(HighLight).order_by(HighLight.create_time.desc(),PostModel.create_time.desc()) elif sort==3: query_obj=PostModel.query.order_by(PostModel.create_time.desc()) elif sort==4: query_obj=db.session.query(PostModel).outerjoin(CommentModel).group_by(PostModel.id).order_by(func.count(CommentModel.id).desc(),PostModel.create_time.desc()) if board_id: query_obj=query_obj.filter(PostModel.board_id==board_id) posts=query_obj.slice(start, end) total=query_obj.count() else: posts = query_obj.slice(start, end) total = query_obj.count() pagination = Pagination(bs_version=3, page=page, total=total, outer_window=0, inner_window=2) context = { 'banners': banners, 'boards': boards, 'posts': posts, 'pagination': pagination, 'current_board':board_id, 'current_sort':sort } return render_template('front/front_index.html', **context)
相关文章
- JavaWeb_(SSH论坛)_六、点赞模块
- 下载论坛源码GBK UTF8 BIG5分别是什么意思
- 开放下载 | 飞天技术峰会-云原生加速应用构建分论坛资料开放下载
- uCOS全家桶PDF文档整理汇总贴,提供论坛,百度云和腾讯云下载(2020-03-09)
- AI:2020年6月24日北京智源大会演讲分享之知识智能专题论坛——12:30-13:10Jure《Recent Advancements in Graph Neural Networks》
- AI:2020年6月24日北京智源大会演讲分享之知识智能专题论坛——10:05-10:50 东昱晓 《Graph Representation Learning》
- AI:2020年6月23日北京智源大会演讲分享之AI创业专题论坛——10:00-10:30张亚勤教授《智能产业新浪潮》
- 基于威胁情报的攻击组织画像与溯源——样本中提取PDB开发路径,进而关注到“neeru”等一些有特殊意义的用户名,然后去社交网站上找人名,关联黑客论坛作者信息,邮件等
- BlockChain:2020年7月10日世界人工智能大会WAIC《链智未来 赋能产业区块链主题论坛》(三)
- BlockChain:《世界经济论坛:区块链将如何重塑金融业?》—20160812—听课笔记