模型

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. Java中的经典算法之快速排序(Quick Sort)

    Java中的经典算法之快速排序(Quick Sort) 快速排序的思想 基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小, 然后再按此方法对 ...

  2. Mybatis接口中传递多个参数

    1.接口 public interface MemberMapper { public boolean insertMember(Members member); public Members sel ...

  3. javascript权威指南第21章 Ajax和Comet

    function createXHR(){ if(typeof XMLHttpRequest !='undefined'){ return new XMLHttpRequest(); }else if ...

  4. kubernetes跨网段pod网络不通问题

    kubernetes跨网段问题 k8s的master是10.10.10.0网段,新加了一些node,网段是172.16.100.0网段,造成容器直接网络不能相互访问. 部署k8s的时候也部署了flan ...

  5. Codeforces Round #459 (Div. 2) C题【思维好题--括号匹配问题】

    题意:给出一个串,只包含 ( ? ) 三种符号,求出有多少个子串是完美匹配的. ( ) ? ) => ( ) ( ) 完美匹配( ( ) ? => ( ( ) )完美匹配? ? ? ? = ...

  6. 转,sql server update set from inner 批量修改的使用

    SQL update select结合语句详解及应用   QL update select语句 最常用的update语法是: 1 2 UPDATE TABLE_NAME SET column_name ...

  7. Greenplum 调优--数据倾斜排查(二)

    上次有个朋友咨询我一个GP数据倾斜的问题,他说查看gp_toolkit.gp_skew_coefficients表时花费了20-30分钟左右才出来结果,后来指导他分析原因并给出其他方案来查看数据倾斜. ...

  8. Cashe的使用

    1.CacheHelper public class CacheHelper { public static ObjectCache Cache { get { return MemoryCache. ...

  9. maven 聚合工程 > 坑总结

    maven聚合工程子项目无法构建:父工程未 install tomcat可以运行,maven 无法编译报错,检查是否添加红色语句: <dependency> <groupId> ...

  10. 常见的HTTP方法有哪些?

    GET:请求指定的页面信息,返回实体主体: HEAD:类似于get请求,只不过返回的响应中没有具体的内容,用于捕获报头: POST:向指定资源提交数据进行处理请求(比如表单提交或者上传文件),.数据被 ...