模型

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系统之帖子加精和取消加精的更多相关文章

  1. 一百四十:CMS系统之使用flask-paginate实现分页功能

    官方文档:https://pythonhosted.org/Flask-paginate/ 安装:pip install flask-paginate 在没有分页的情况下,默认会加载所有内容 在con ...

  2. 第三百四十五节,Python分布式爬虫打造搜索引擎Scrapy精讲—爬虫和反爬的对抗过程以及策略—scrapy架构源码分析图

    第三百四十五节,Python分布式爬虫打造搜索引擎Scrapy精讲—爬虫和反爬的对抗过程以及策略—scrapy架构源码分析图 1.基本概念 2.反爬虫的目的 3.爬虫和反爬的对抗过程以及策略 scra ...

  3. Flask实战第64天:帖子加精和取消加精功能完成

    帖子加精和取消加精是在cms后台来设置的 后台逻辑 首页个帖子加精设计个模型表,编辑apps.models.py class HighlightPostModel(db.Model): __table ...

  4. 一百四十六:CMS系统之帖子按照发布时间和评论数量排序

    按照不同选项进行排序 视图 @bp.route('/')def index(): board_id = request.args.get('board_id', type=int, default=N ...

  5. 一百四十二:CMS系统之帖子详情页面布局

    定义一个404页面 <!DOCTYPE html><html lang="en"><head> <meta charset="U ...

  6. 一百四十一:CMS系统之根据板块过滤显示帖子

    视图,根据传过来的板块id查数据 @bp.route('/')def index(): board_id = request.args.get('board_id', type=int, defaul ...

  7. 完成将 toChineseNum, 可以将数字转换成中文大写的表示,处理到万级别,例如 toChineseNum(12345),返回 一万二千三百四十五

    const toChineseNum = (num) => { const unit = ['', '十', '百', '千'] const counts = ['零', '一', '二', ' ...

  8. 一百一十:CMS系统之剩余菜单栏的页面和视图

    增加所有剩余菜单的页面,并用视图渲染,方便后面调试权限控制 {% extends 'cms/cms_base.html' %} {% block title %}板块管理{% endblock %} ...

  9. 一百四十三:CMS系统之评论布局和功能一

    模型 class CommentModel(db.Model): """ 评论 """ __tablename__ = 'comment' ...

随机推荐

  1. async/await中reject的问题

    promise 返回的 resolve 对象可能用 await 去接,但是 reject 无法用 await 接收到,所以要用 try catch 去处理 例如发送邮件的接口设置: async fun ...

  2. Hibernate初探之单表映射——jar包的导入

    编写第一个Hibernate例子需要的基本步骤 创建Hibernate的配置文件 创建持久化类 创建对象-关系映射文件 通过Hibernate API编写访问数据库的代码 使用版本:Hibernate ...

  3. LeetCode 339. Nested List Weight Sum

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...

  4. HTML 001 入门介绍

    HTML 教程- (HTML5 标准) 超文本标记语言(英语:HyperText Markup Language,简称:HTML)是一种用于创建网页的标准标记语言. 您可以使用 HTML 来建立自己的 ...

  5. ashx 图片上传

    为了方便多出调用图片上传方法       首先我们将图片上传方法抽离出来 创建ashx 一个新的方法 using System; using System.Collections.Generic; u ...

  6. jQuery的ajax()方法提交数组问题

    http://blog.csdn.net/thc1987/article/details/7278269 解决办法是添加一个属性 traditional:true $.ajax({    type: ...

  7. c++学生信息管理系统(window控制台实现鼠标点击操作)

    翻起大一时写过的作业代码--一个学生信息管理系统,当时不会使用QT,不会MFC等库,只会c++,但是又想做一个有界面的,能够实现鼠标操作的程序.于是绞尽脑汁查资料,自己造轮子,最终写出来了下面的这个现 ...

  8. JDBC_MySQL8.0.13_连接测试

    前言 手贱把MySQL升级到了8.0.13,在IntelliJ IDEA中测试连接不上.因此记录一下,供个人以后参考. 系统环境 win10x64 jkd11 IDEA MySQL 8.10.13 C ...

  9. Python中一些高效的数据操作

    列表统计 chars = ["a", "b", "a", "c", "a", "d&quo ...

  10. SQL - where条件里的!=会过滤值为null的数据

    !=会过滤值为null的数据 在测试数据时忽然发现,使用如下的SQL是无法查询到对应column为null的数据的: select * from test where name != 'Lewis'; ...