一百四十五:CMS系统之帖子加精和取消加精
模型
class HighlightPostModel(db.Model):
""" 帖子加精信息 """
__tablename__ = 'highlight_post'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
create_time = db.Column(db.DateTime, default=datetime.now)
post = db.relationship('PostModel', backref='highlight')
执行数据库迁移:
python manager.py db migrate
python manager.py db upgrade
后台渲染帖子
视图
@bp.route('/posts/')
@login_required
@permission_required(CMSPersmission.POSTER)
def posts():
""" 帖子管理板块 """
context = {'posts': PostModel.query.all()}
return render_template('cms/cms_posts.html', **context)
页面
{% extends 'cms/cms_base.html' %} {% block title %}帖子管理{% endblock %} {% block head %} {% endblock %} {% block page_title %}
{{ self.title() }}
{% endblock %} {% block main_content %}
<table class="table table-bordered">
<thead>
<tr>
<th>标题</th>
<th>发布时间</th>
<th>板块</th>
<th>作者</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for post in posts %}
<tr>
<td><a target="_blank" href="{{ url_for('front.post_detail', post_id=post.id) }}">{{ post.title }}</a></td>
<td>{{ post.create_time }}</td>
<td>{{ post.board.name }}</td>
<td>{{ post.author.username }}</td>
<td>
{% if post.highlight %}
<button class="btn btn-default btn-xs">取消加精</button>
{% else %}
<button class="btn btn-default btn-xs">加精</button>
{% endif %}
<button class="btn btn-danger btn-xs">移除</button>
</td>
</tr>
{% endfor %} </tbody>
</table>
{% endblock %}
加精和取消加精视图
@bp.route('/hpost/', methods=['POST'])
@login_required
@permission_required(CMSPersmission.POSTER)
def hpost():
""" 帖子加精 """
post_id = request.form.get('post_id')
if not post_id:
return restful.params_error('请传入帖子id')
post = PostModel.query.get(post_id)
if not post:
return restful.params_error('未找到帖子')
highlight = HighlightPostModel()
highlight.post = post
db.session.add(highlight)
db.session.commit()
return restful.success('加精成功') @bp.route('/uhpost/', methods=['POST'])
@login_required
@permission_required(CMSPersmission.POSTER)
def uhpost():
""" 帖子取消加精 """
post_id = request.form.get('post_id')
if not post_id:
return restful.params_error('请传入帖子id')
post = PostModel.query.get(post_id)
if not post:
return restful.params_error('未找到帖子')
highlight = HighlightPostModel.query.filter_by(post_id=post_id).first()
db.session.delete(highlight)
db.session.commit()
return restful.success('取消加精成功')
html
{% extends 'cms/cms_base.html' %}
{% from 'common/_macros.html' import static %} {% block title %}帖子管理{% endblock %} {% block head %}
<script src="{{ static('cms/js/posts.js') }}"></script>
{% endblock %} {% block page_title %}
{{ self.title() }}
{% endblock %} {% block main_content %}
<table class="table table-bordered">
<thead>
<tr>
<th>标题</th>
<th>发布时间</th>
<th>板块</th>
<th>作者</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for post in posts %}
{#方便js判断,将帖子信息绑定到tr标签上, data-highlight=1,则此贴已被加精#}
<tr data-id="{{ post.id }}" data-highlight="{{ 1 if post.highlight else 0 }}">
<td><a target="_blank" href="{{ url_for('front.post_detail', post_id=post.id) }}">{{ post.title }}</a></td>
<td>{{ post.create_time }}</td>
<td>{{ post.board.name }}</td>
<td>{{ post.author.username }}</td>
<td>
{% if post.highlight %}
<button class="btn btn-default btn-xs highlight-btn">取消加精</button>
{% else %}
<button class="btn btn-default btn-xs highlight-btn">加精</button>
{% endif %}
<button class="btn btn-danger btn-xs">移除</button>
</td>
</tr>
{% endfor %} </tbody>
</table>
{% endblock %}
js
$(function () {
$('.highlight-btn').click(function () {
var self = $(this);
var tr = self.parent().parent();
var post_id = tr.attr('data-id');
var highlight = parseInt(tr.attr('data-highlight')); //转int
if(highlight){
url = '/cms/uhpost/';
}else{
url = '/cms/hpost/';
}
ajax.post({
'url': url,
'data': {
'post_id': post_id
},
'success': function (data) {
if(data['code'] == 200){
xtalert.alertSuccessToast('操作成功');
// 等待500毫秒再刷新浏览器
setTimeout(function () {
window.location.reload();
}, 500);
}else{
xtalert.alertInfo(data['message']);
}
}
});
});
});
效果
一百四十五:CMS系统之帖子加精和取消加精的更多相关文章
- 一百四十:CMS系统之使用flask-paginate实现分页功能
官方文档:https://pythonhosted.org/Flask-paginate/ 安装:pip install flask-paginate 在没有分页的情况下,默认会加载所有内容 在con ...
- 第三百四十五节,Python分布式爬虫打造搜索引擎Scrapy精讲—爬虫和反爬的对抗过程以及策略—scrapy架构源码分析图
第三百四十五节,Python分布式爬虫打造搜索引擎Scrapy精讲—爬虫和反爬的对抗过程以及策略—scrapy架构源码分析图 1.基本概念 2.反爬虫的目的 3.爬虫和反爬的对抗过程以及策略 scra ...
- Flask实战第64天:帖子加精和取消加精功能完成
帖子加精和取消加精是在cms后台来设置的 后台逻辑 首页个帖子加精设计个模型表,编辑apps.models.py class HighlightPostModel(db.Model): __table ...
- 一百四十六:CMS系统之帖子按照发布时间和评论数量排序
按照不同选项进行排序 视图 @bp.route('/')def index(): board_id = request.args.get('board_id', type=int, default=N ...
- 一百四十二: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 ...
- 完成将 toChineseNum, 可以将数字转换成中文大写的表示,处理到万级别,例如 toChineseNum(12345),返回 一万二千三百四十五
const toChineseNum = (num) => { const unit = ['', '十', '百', '千'] const counts = ['零', '一', '二', ' ...
- 一百一十:CMS系统之剩余菜单栏的页面和视图
增加所有剩余菜单的页面,并用视图渲染,方便后面调试权限控制 {% extends 'cms/cms_base.html' %} {% block title %}板块管理{% endblock %} ...
- 一百四十三:CMS系统之评论布局和功能一
模型 class CommentModel(db.Model): """ 评论 """ __tablename__ = 'comment' ...
随机推荐
- python listdir() 中文路径 中文文件夹 乱码 解决方法
python listdir() 中文路径 中文文件夹 乱码 解决方法 listdir(path)返回的结果的编码似乎和我们提供的 path 参数的编码有关: path = 'd:/test' try ...
- 0022SpringMVC解决post请求中文乱码的问题
我们在页面难免提交一些中文数据给后台处理,但是发现后台拿到的数据乱码,可以在每一个controller中都设置编码,但是太过于麻烦, 正确的解决办法应该是在web.xml中配置解决中文乱码的过滤器: ...
- go if for while 的使用
fileName := "a.txt"contents ,err := ioutil.ReadFile(fileName) if err != nil{ fmt.Println(& ...
- celery timeout的拦截
0X01 场景 celery任务超时报错,想查看是传入哪一类数据运行时导致的超时(哪一个插件),但是该报错难以拦截. [2019-06-30 17:23:21,070: ERROR/MainProce ...
- [cf1140D. Minimum Triangulation][dp]
D. Minimum Triangulation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 有关求第n位xxx 的算法的问题
最近,博客园上看到有关求 斐波那契数列的第n位是什么的问题.什么是 斐波那契数列? 我自己也忘记了,后来百度了下.http://baike.baidu.com/view/816.htm?fr=alad ...
- Spring的注解@Qualifier用法
Spring的注解@Qualifier用法在Controller中需要注入service那么我的这个server有两个实现类如何区分开这两个impl呢?根据注入资源的注解不同实现的方式有一点小小的区别 ...
- Greenplum最佳实践
一.最佳实践数据库参数部分 GP数据库参数配置 以下配置存于文件–postgresql.conf中,仅列出一些最常用的参数. shared_buffers:刚开始可以设置一个较小的值,比如总内存的15 ...
- qt 读取xml文件中文问题
1.保存文件格式为UTF-8 2.文件流打开时设置 QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) { qDebug() < ...
- meshing-八分之一圆球
原视频下载地址:https://yunpan.cn/cqwiFDCg6PbFj 访问密码 d1c8