一百四十六:CMS系统之帖子按照发布时间和评论数量排序
按照不同选项进行排序
视图

@bp.route('/')
def index():
board_id = request.args.get('board_id', type=int, default=None)
banners = BannerModel.query.order_by(BannerModel.priority.desc()).limit(4) # 只取4条
boards = BoardModel.query.all()
sort = request.args.get('sort', type=int, default=1) # 排序方式:精华帖子、点赞最多、评论最多
page = request.args.get(get_page_parameter(), type=int, default=1)
start = (page - 1) * config.PER_PAGE
end = start + config.PER_PAGE
if sort == 1: # 默认时间倒序
query_obj = PostModel.query.order_by(PostModel.create_time.desc())
elif sort == 2: # 加精时间倒序
query_obj = db.session.query(PostModel).outerjoin(HighlightPostModel).order_by(
HighlightPostModel.create_time.desc(), PostModel.create_time.desc())
elif sort == 3: # todo 点赞数量倒序
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_by(board_id=board_id)
posts = query_obj.slice(start, end)
total = query_obj.count()
else:
posts = query_obj.slice(start, end)
total = PostModel.query.count()
# bs_version: bootstrap版本
pagination = Pagination(bs_version=3, page=page, total=total)
context = {'banners': banners, 'boards': boards, 'posts': posts, 'pagination': pagination,
'current_board': board_id, # 把board_id传给前端用于渲染选中事件
'current_sort': sort} # 把sort传给前端用于渲染选中事件
return render_template('front/front_index.html', **context)
前端

{% extends 'front/front_base.html' %}
{% from "common/_macros.html" import static %}
{% block title %}
首页
{% endblock %}
{% block head %}
<link rel="stylesheet" href="{{ static('front/css/front_index.css') }}">
{% endblock %}
{% block body %}
<div class="lg-container">
<!-- 轮播图 -->
<div id="carousel-example-generic" class="carousel slide index-banner" data-ride="carousel">
<!-- 指示器,轮播图下方的圆圈,需与轮播图数量一致 -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
<li data-target="#carousel-example-generic" data-slide-to="3"></li>
</ol>
<!-- 轮播图 这里的图片是在百度复制的轮播图链接-->
<div class="carousel-inner" role="listbox">
{% for banner in banners %}
{% if loop.first %}
<div class="item active">
{% else %}
<div class="item">
{% endif %}
<a href="{{ banner.link_url }}"><img src="{{ banner.image_url }}" alt="{{ banner.name }}"></a>
</div>
{% endfor %}
</div>
<!-- 左右切换的控制按钮 -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<div class="post-group">
<ul class="post-group-head">
{#默认排序按发布时间,2:精华帖子、3:点赞最多、4:评论最多#}
{% if current_sort == 1 %}
<li class="active"><a href="/">最新</a></li>
{% else %}
<li><a href="/">最新</a></li>
{% endif %}
{% if current_sort == 2 %}
<li class="active"><a href="{{ url_for('front.index', sort=2) }}">精华帖子</a></li>
{% else %}
<li><a href="{{ url_for('front.index', sort=2) }}">精华帖子</a></li>
{% endif %}
{% if current_sort == 3 %}
<li class="active"><a href="{{ url_for('front.index', sort=3) }}">点赞最多</a></li>
{% else %}
<li><a href="{{ url_for('front.index', sort=3) }}">点赞最多</a></li>
{% endif %}
{% if current_sort == 4 %}
<li class="active"><a href="{{ url_for('front.index', sort=4) }}">评论最多</a></li>
{% else %}
<li><a href="{{ url_for('front.index', sort=4) }}">评论最多</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 static('common/images/logo.png') }}" alt="">
</div>
<div class="posst-info-group">
<a href="{{ url_for('front.post_detail', post_id=post.id) }}" class="post-title">{{ post.title }}</a>
{% if post.highlight %}
<span class="label label-danger">精华帖</span>
{% endif %}
<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>
<div class="text-center">
{{ pagination.links }}
</div>
</div>
</div>
<div class="sm-container">
<div style="padding-bottom: 10px;">
<a href="{{ url_for('front.apost') }}" class="btn btn-warning btn-block">发布帖子</a>
</div>
<div class="list-group">
{% if current_board %}
<a href="/" class="list-group-item">所有板块</a>
{% else %}
<a href="/" class="list-group-item active">所有板块</a>
{% endif %}
{% for board in boards %}
{% if board.id == current_board %}
<a href="{{ url_for('front.index', board_id=board.id) }}" class="list-group-item active">{{ board.name }}</a>
{% else %}
<a href="{{ url_for('front.index', board_id=board.id) }}" class="list-group-item">{{ board.name }}</a>
{% endif %}
{% endfor %}
</div>
</div>
{% endblock %}
效果


根据板块+选项排序

{% extends 'front/front_base.html' %}
{% from "common/_macros.html" import static %}
{% block title %}
首页
{% endblock %}
{% block head %}
<link rel="stylesheet" href="{{ static('front/css/front_index.css') }}">
{% endblock %}
{% block body %}
<div class="lg-container">
<!-- 轮播图 -->
<div id="carousel-example-generic" class="carousel slide index-banner" data-ride="carousel">
<!-- 指示器,轮播图下方的圆圈,需与轮播图数量一致 -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
<li data-target="#carousel-example-generic" data-slide-to="3"></li>
</ol>
<!-- 轮播图 这里的图片是在百度复制的轮播图链接-->
<div class="carousel-inner" role="listbox">
{% for banner in banners %}
{% if loop.first %}
<div class="item active">
{% else %}
<div class="item">
{% endif %}
<a href="{{ banner.link_url }}"><img src="{{ banner.image_url }}" alt="{{ banner.name }}"></a>
</div>
{% endfor %}
</div>
<!-- 左右切换的控制按钮 -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<div class="post-group">
<ul class="post-group-head">
{#默认排序按发布时间,2:精华帖子、3:点赞最多、4:评论最多#}
{% if current_sort == 1 %}
<li class="active"><a href="{{ url_for('front.index', sort=1,board_id=current_board) }}">最新</a></li>
{% else %}
<li><a href="{{ url_for('front.index', sort=1,board_id=current_board) }}">最新</a></li>
{% endif %}
{% if current_sort == 2 %}
<li class="active"><a href="{{ url_for('front.index', sort=2,board_id=current_board) }}">精华帖子</a></li>
{% else %}
<li><a href="{{ url_for('front.index', sort=2,board_id=current_board) }}">精华帖子</a></li>
{% endif %}
{% if current_sort == 3 %}
<li class="active"><a href="{{ url_for('front.index', sort=3,board_id=current_board) }}">点赞最多</a></li>
{% else %}
<li><a href="{{ url_for('front.index', sort=3,board_id=current_board) }}">点赞最多</a></li>
{% endif %}
{% if current_sort == 4 %}
<li class="active"><a href="{{ url_for('front.index', sort=4,board_id=current_board) }}">评论最多</a></li>
{% else %}
<li><a href="{{ url_for('front.index', sort=4,board_id=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 static('common/images/logo.png') }}" alt="">
</div>
<div class="posst-info-group">
<a href="{{ url_for('front.post_detail', post_id=post.id) }}" class="post-title">{{ post.title }}</a>
{% if post.highlight %}
<span class="label label-danger">精华帖</span>
{% endif %}
<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>
<div class="text-center">
{{ pagination.links }}
</div>
</div>
</div>
<div class="sm-container">
<div style="padding-bottom: 10px;">
<a href="{{ url_for('front.apost') }}" class="btn btn-warning btn-block">发布帖子</a>
</div>
<div class="list-group">
{% if current_board %}
<a href="/" class="list-group-item">所有板块</a>
{% else %}
<a href="/" class="list-group-item active">所有板块</a>
{% endif %}
{% for board in boards %}
{% if board.id == current_board %}
<a href="{{ url_for('front.index', board_id=board.id) }}" class="list-group-item active">{{ board.name }}</a>
{% else %}
<a href="{{ url_for('front.index', board_id=board.id) }}" class="list-group-item">{{ board.name }}</a>
{% endif %}
{% endfor %}
</div>
</div>
{% endblock %}

效果


一百四十六:CMS系统之帖子按照发布时间和评论数量排序的更多相关文章
- Flask实战第65天:帖子按照发布时间和评论数量等排序
排序,我们需要在前端传递参数, 编辑front_index.html 编辑front.views.py from apps.models import HighlightPostModel from ...
- 一百四十:CMS系统之使用flask-paginate实现分页功能
官方文档:https://pythonhosted.org/Flask-paginate/ 安装:pip install flask-paginate 在没有分页的情况下,默认会加载所有内容 在con ...
- [转]PostgreSQL教程(十六):系统视图详解
这篇文章主要介绍了PostgreSQL教程(十六):系统视图详解,本文讲解了pg_tables.pg_indexes.pg_views.pg_user.pg_roles.pg_rules.pg_set ...
- 第三百四十六节,Python分布式爬虫打造搜索引擎Scrapy精讲—Requests请求和Response响应介绍
第三百四十六节,Python分布式爬虫打造搜索引擎Scrapy精讲—Requests请求和Response响应介绍 Requests请求 Requests请求就是我们在爬虫文件写的Requests() ...
- 一百四十五:CMS系统之帖子加精和取消加精
模型 class HighlightPostModel(db.Model): """ 帖子加精信息 """ __tablename__ = ...
- 一百四十二:CMS系统之帖子详情页面布局
定义一个404页面 <!DOCTYPE html><html lang="en"><head> <meta charset="U ...
- 一百四十一:CMS系统之根据板块过滤显示帖子
视图,根据传过来的板块id查数据 @bp.route('/')def index(): board_id = request.args.get('board_id', type=int, defaul ...
- 一百一十:CMS系统之剩余菜单栏的页面和视图
增加所有剩余菜单的页面,并用视图渲染,方便后面调试权限控制 {% extends 'cms/cms_base.html' %} {% block title %}板块管理{% endblock %} ...
- Mac OS使用技巧之十六:系统失去响应怎么办?
再好的系统,再快的本本,也会在执行时由于种种原因出现卡顿或者死机等失去响应的情况.Mac用户也会时不时碰到这样的情况,最常见的表现为鼠标变为七彩圆圈.通常等上一会儿系统会自己恢复.假设迟迟没有响应的话 ...
随机推荐
- Notepad++ 连接远程 NppFTP
远程连接 1.自动安装: 插件——> Plugin Manager——>ShowPlugin Manager——>勾选NppFTP插件——>Install 2. 手动安装 ...
- 2. kafka
目录: 1.kafka概念 2.kafka使用场景 3.相关术语 4.原理解析 5.项目实战 一. kafka是什么 https://www.jianshu.com/p/014af2b34159 Ka ...
- SQL进阶17-变量的声明/使用(输出)--全局变量/会话变量--用户变量/局部变量
/*进阶17 变量 系统变量: 全局变量: 会话变量: 自定义变量: 用户变量: 局部变量: */ /* #一: 系统变量 #说明: 变量由系统提供,不是用户定义的,属于服务器层面 #使用的语法 #1 ...
- git 获取 remote 的 url
git 获取 remote 的 url git ls-remote --get-url [remote] 例如: git ls-remote --get-url origin
- 设计模式之命令模式-JS
理解命令模式 假设有一个快餐店,而我是该餐厅的点餐服务员,那么我一天的工作应该是这样的:当某位客人点餐或者打来订餐电话后,我会把他的需求都写在清单上,然后交给厨房,客人不用关心是哪些厨师帮他炒菜.我们 ...
- Robot Framework--Scalar变量
一.变量赋值 1)Set赋值 通常使用Set Variable关键字对变量进行赋值,其他Set相关的带Variable的关键字也可以进行赋值 赋值的时候,变量后面写不写『=』都可以,如下: 如果${v ...
- postgresql —— 查看索引
查索引 语句: SELECT tablename, indexname, indexdef FROM pg_indexes WHERE tablename = 'user_tbl' ORDER BY ...
- java中的finally用法总结
不管 try 语句块正常结束还是异常结束,finally 语句块是保证要执行的.如果 try 语句块正常结束,那么在 try 语句块中的语句都执行完之后,再执行 finally 语句块.如果 try ...
- 15 Vue计算属性和侦听器
计算属性 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的. 在模板中放入太多的逻辑会让模板过重且难以维护.例如: split = 字符中间空格分割, reverse= 反转 join('' ...
- 黑魔法师之门 (magician)-并查集
题目 经过了 16 个工作日的紧张忙碌,未来的人类终于收集到了足够的能源.然而在与 Violet 星球的战争中,由于 Z 副官的愚蠢,地球的领袖 applepi 被邪恶的黑魔法师 Vani 囚禁在了 ...