BBS论坛(三十二)
2025-01-15 02:41:39

LIKE.TG 成立于2020年,总部位于马来西亚,是首家汇集全球互联网产品,提供一站式软件产品解决方案的综合性品牌。唯一官方网站:www.like.tg
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)

现在关注【LIKE.TG出海指南频道】、【LIKE.TG生态链-全球资源互联社区】,即可免费领取【WhatsApp、LINE、Telegram、Twitter、ZALO云控】等获客工具试用、【住宅IP、号段筛选】等免费资源,机会难得,快来解锁更多资源,助力您的业务飞速成长!点击【联系客服】
本文由LIKE.TG编辑部转载自互联网并编辑,如有侵权影响,请联系官方客服,将为您妥善处理。
This article is republished from public internet and edited by the LIKE.TG editorial department. If there is any infringement, please contact our official customer service for proper handling.