帖子加精和取消加精是在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. PHP系统编程--02.PHP守护进程化

    什么是守护进程? 一个守护进程通常补认为是一个不对终端进行控制的后台任务.它有三个很显著的特征:在后台运行,与启动他的进程脱离,无须控制终端.常用的实现方式是fork() -> setsid() ...

  2. 通过gitlabAPI批量创建用户

    上午服务器领导通知我给服务器所有同事添加gitlab账号,服务器总共67个人,这要是一个一个在页面添加,我得累死,是否有其他的办法呢?有问题找google,果然是可以通过gitlab的API批量添加的 ...

  3. redhat 7 配置yum本地源

    http://www.unixarena.com/2015/04/how-to-create-the-yum-repository-on-rhel-7.html   1. 在虚拟机上挂上cd 2. m ...

  4. 【51NOD-5】1293 球与切换器

    [算法]DP [题解]f[i][j][0]表示在i,j位置往下走的球数,f[i][j][1]表示在i,j位置往右走的球数,经过i,j的球若为-1则(num+1)/2往下,其余往右.+1类似. 转移见代 ...

  5. NSObject class和NSObject protocol的关系(抽象基类与协议)

    [转载请注明出处] 1.接口的实现 对于接口这一概念的支持,不同语言的实现形式不同.Java中,由于不支持多重继承,因此提供了一个Interface关键词.而在C++中,通常是通过定义抽象基类的方式来 ...

  6. bzoj 2258 splay

    类似于1014,用splay维护这个序列,维护每个节点为根的子树的hash值,对于一个询问二分答案判断就行了. 反思:询问的时候因为是原序列的x,y,所以开始的时候直接splay(x-1)了,后来发现 ...

  7. Spring Boot提供的特性

    一.导览 本文主要按以下模块介绍spring Boot(1.3.6.RELEASE)提供的特性. SpringApplication类 外部化配置 Profiles 日志 开发WEB应用 Securi ...

  8. Eclipse中SVN更改连接用户

    Eclipse中安装了SVN插件,当连接到SVN服务器后,便无法从客户端更改连接帐号 百度一下,也就知道 查看Eclipse中使用的是什么SVN Interface,位置在windows > p ...

  9. 【Python学习笔记】多版本python使用pip安装第三方库

    不知道是不是有人跟我一样,一直Python2与Python3混着用,然而在cmd中默认的Python版本只有一种,使用 pip install xxx(第三方库名)  只会安装到默认版本上. 而如果需 ...

  10. Laravel 调试器 Debugbar 和数据库导出利器 DbExporter 扩展安装及注意事项

    一.Debugbar安装 参考:Laravel 调试利器 —— Laravel Debugbar 扩展包安装及使用教程 的“2.安装”部分 二.DbExporter安装 参考:Laravel 扩展推荐 ...