帖子加精和取消加精是在cms后台来设置的

后台逻辑

首页个帖子加精设计个模型表,编辑apps.models.py

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 manage.py db migrate
python manage.py db upgrade

视图函数,编辑cms.views.py

from apps.models import HighlightPostModel, PostModel
... @bp.route('/posts/')
@login_required
@permission_required(CMSPersmission.POSTER)
def posts():
post_list = PostModel.query.all()
return render_template('cms/cms_posts.html', posts=post_list) @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 xjson.json_param_error('请传入帖子id!')
post = PostModel.query.get(post_id)
if not post:
return xjson.json_param_error("没有这篇帖子!") highlight = HighlightPostModel()
highlight.post = post
db.session.add(highlight)
db.session.commit()
return xjson.json_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 xjson.json_param_error('请传入帖子id!')
post = PostModel.query.get(post_id)
if not post:
return xjson.json_param_error("没有这篇帖子!") highlight = HighlightPostModel.query.filter_by(post_id=post_id).first()
db.session.delete(highlight)
db.session.commit()
return xjson.json_success()

cms.views.py

前端逻辑完成

{% extends 'cms/cms_base.html' %}

{% block title %}
帖子管理-CMS管理系统
{% endblock %} {% block page_title %}
帖子管理
{% endblock %} {% block head %}
<script src="{{ url_for('static', filename='cms/js/posts.js')}}"></script>
{% 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 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 %}

cms_posts.html

$(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"));
var url = "";
if(highlight){
url = "/cms/uhpost/";
}else{
url = "/cms/hpost/";
}
bbsajax.post({
'url': url,
'data': {
'post_id': post_id
},
'success': function (data) {
if(data['code'] == 200){
xtalert.alertSuccessToast('操作成功!');
setTimeout(function () {
window.location.reload();
},500);
}else{
xtalert.alertInfo(data['message']);
}
}
});
});
});

posts.js

前台页面,要加精的帖子加个标签,编辑front_index.html

Flask实战第64天:帖子加精和取消加精功能完成的更多相关文章

  1. 一百四十五:CMS系统之帖子加精和取消加精

    模型 class HighlightPostModel(db.Model): """ 帖子加精信息 """ __tablename__ = ...

  2. Flask实战-留言板-安装虚拟环境、使用包组织代码

    Flask实战 留言板 创建项目目录messageboard,从GreyLi的代码中把Pipfile和Pipfile.lock文件拷贝过来,这两个文件中定义了虚拟环境中需要安装的包的信息和位置,进入m ...

  3. ASP.NET MVC深入浅出(被替换) 第一节: 结合EF的本地缓存属性来介绍【EF增删改操作】的几种形式 第三节: EF调用普通SQL语句的两类封装(ExecuteSqlCommand和SqlQuery ) 第四节: EF调用存储过程的通用写法和DBFirst模式子类调用的特有写法 第六节: EF高级属性(二) 之延迟加载、立即加载、显示加载(含导航属性) 第十节: EF的三种追踪

    ASP.NET MVC深入浅出(被替换)   一. 谈情怀-ASP.NET体系 从事.Net开发以来,最先接触的Web开发框架是Asp.Net WebForm,该框架高度封装,为了隐藏Http的无状态 ...

  4. Android中ViewPager+Fragment取消(禁止)预加载延迟加载(懒加载)问题解决方案

    转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/53205878本文出自[DylanAndroid的博客] Android中Vie ...

  5. EF如何操作内存中的数据以及加载相关联表的数据:延迟加载、贪婪加载、显示加载

    之前的EF Code First系列讲了那么多如何配置实体和数据库表的关系,显然配置只是辅助,使用EF操作数据库才是每天开发中都需要用的,这个系列讲讲如何使用EF操作数据库.老版本的EF主要是通过Ob ...

  6. jquery mobile在页面加载时添加加载中效果 document.ready 和window.onload执行顺序比较

    想要添加这个效果,先来弄明白页面的加载和事件执行顺序,看这个简单例子: <html xmlns="http://www.w3.org/1999/xhtml"> < ...

  7. entity framework 数据加载三种方式的异同(延迟加载,预加载,显示加载)

    三种加载方式的区别 显示加载: 显示加载

  8. 实战使用Axure设计App,使用WebStorm开发(5) – 实现页面功能

    系列文章 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求  实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目   实战使 ...

  9. javascript图片懒加载与预加载的分析

    javascript图片懒加载与预加载的分析 懒加载与预加载的基本概念.  懒加载也叫延迟加载:前一篇文章有介绍:JS图片延迟加载 延迟加载图片或符合某些条件时才加载某些图片. 预加载:提前加载图片, ...

随机推荐

  1. 最简单的图文教程,几步完成Git的公私钥配置

    操作的前提是,你有git账号,并且在自己的电脑上已经装好了TorToiseGit工具.下面开始简单的教程: 第一步:Windows-->所有程序-->TortoiseGit-->Pu ...

  2. 【CodeForces】835D Palindromic characteristics

    [题意]给你一个串,让你求出k阶回文子串有多少个.k从1到n.k阶子串的定义是:子串本身是回文串,而且它的左半部分也是回文串. [算法]区间DP [题解]涉及回文问题的区间DP都可以用类似的写法,就是 ...

  3. 【洛谷 P1390】 公约数的和 (欧拉函数)

    题目链接 做过\(n\)遍这种题了... 答案就是\(\sum_{i=1}^{n}\sum_{j=1}^{n/i}[\varphi(j)*i]\) 线筛欧拉函数求前缀和直接算就行. #include ...

  4. 【洛谷 T47488】 D:希望 (点分治)

    题目链接 看到这种找树链的题目肯定是想到点分治的. 我码了一下午,\(debug\)一晚上,终于做到只有两个点TLE了. 我的是不完美做法 加上特判\(A\)了这题qwq 记录每个字母在母串中出现的所 ...

  5. Python 语言使用中遇到的问题汇总

    1.python中的传值和传引用 和其他语言不一样,传递参数的时候,python不允许程序员选择采用传值还是传引用.Python参数传递采用的肯定是“传对象引用”的方式.实际上,这种方式相当于传值和传 ...

  6. DesignPattern

    目录

  7. Webmin LFD to LFI

    Webmin < 1.290 / Usermin < 1.220 - Arbitrary File Disclosure (Perl) https://www.exploit-db.com ...

  8. mysql增删

    create table msg (id int, name varchar(10)); 插入语句 insert into msg values(1,'root'); insert into msg( ...

  9. Centos7的iso everything与DVD以及Live的区别

    DVD.ISO 可以用安装程序安装的所有安装包,推荐镜像 Netinstall.iso 从网络安装或者救援系统 Everything.iso 包含centos7的一套完整的软件包,可以用来安装系统或者 ...

  10. python基础===pendulum '''Python datetimes made easy.'''

    https://pypi.python.org/pypi/pendulum Pendulum的一大优势是内嵌式取代Python的datetime类,可以轻易地将它整合进已有代码,并且只在需要的时候才进 ...