Flask实战第64天:帖子加精和取消加精功能完成
帖子加精和取消加精是在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天:帖子加精和取消加精功能完成的更多相关文章
- 一百四十五:CMS系统之帖子加精和取消加精
模型 class HighlightPostModel(db.Model): """ 帖子加精信息 """ __tablename__ = ...
- Flask实战-留言板-安装虚拟环境、使用包组织代码
Flask实战 留言板 创建项目目录messageboard,从GreyLi的代码中把Pipfile和Pipfile.lock文件拷贝过来,这两个文件中定义了虚拟环境中需要安装的包的信息和位置,进入m ...
- ASP.NET MVC深入浅出(被替换) 第一节: 结合EF的本地缓存属性来介绍【EF增删改操作】的几种形式 第三节: EF调用普通SQL语句的两类封装(ExecuteSqlCommand和SqlQuery ) 第四节: EF调用存储过程的通用写法和DBFirst模式子类调用的特有写法 第六节: EF高级属性(二) 之延迟加载、立即加载、显示加载(含导航属性) 第十节: EF的三种追踪
ASP.NET MVC深入浅出(被替换) 一. 谈情怀-ASP.NET体系 从事.Net开发以来,最先接触的Web开发框架是Asp.Net WebForm,该框架高度封装,为了隐藏Http的无状态 ...
- Android中ViewPager+Fragment取消(禁止)预加载延迟加载(懒加载)问题解决方案
转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/53205878本文出自[DylanAndroid的博客] Android中Vie ...
- EF如何操作内存中的数据以及加载相关联表的数据:延迟加载、贪婪加载、显示加载
之前的EF Code First系列讲了那么多如何配置实体和数据库表的关系,显然配置只是辅助,使用EF操作数据库才是每天开发中都需要用的,这个系列讲讲如何使用EF操作数据库.老版本的EF主要是通过Ob ...
- jquery mobile在页面加载时添加加载中效果 document.ready 和window.onload执行顺序比较
想要添加这个效果,先来弄明白页面的加载和事件执行顺序,看这个简单例子: <html xmlns="http://www.w3.org/1999/xhtml"> < ...
- entity framework 数据加载三种方式的异同(延迟加载,预加载,显示加载)
三种加载方式的区别 显示加载: 显示加载
- 实战使用Axure设计App,使用WebStorm开发(5) – 实现页面功能
系列文章 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求 实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目 实战使 ...
- javascript图片懒加载与预加载的分析
javascript图片懒加载与预加载的分析 懒加载与预加载的基本概念. 懒加载也叫延迟加载:前一篇文章有介绍:JS图片延迟加载 延迟加载图片或符合某些条件时才加载某些图片. 预加载:提前加载图片, ...
随机推荐
- bzoj 2144: 跳跳棋——倍增/二分
Description 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子.我们用跳跳棋来做一个简单的游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置.我们要通过最少的跳动把他 ...
- bzoj 1034 贪心
首先如果我们想取得分最高的话,肯定尽量赢,实在赢不了的话就耗掉对方最高的,那么就有了贪心策略,先排序,我方最弱的马和敌方最弱的相比,高的话赢掉,否则耗掉敌方最高的马. 对于一场比赛,总分是一定的,所以 ...
- 自定义ToolBar
一.Toolbar的简介 Toolbar 是 android 5.0 引入的一个新控件,Toolbar出现之前,我们很多时候都是使用ActionBar以及ActionActivity实现顶部导航栏的, ...
- nginx 伪静态rewrite
location正则写法 一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所 ...
- [Leetcode Week13]Search a 2D Matrix
Search a 2D Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/ D ...
- jython
# -*- coding: utf-8 -*- import sys import json sys.path += ["C:/Users/yangbo/Desktop/restassure ...
- 105.Construct Binary Tree from Preorder and Inorder Traversal---《剑指offer》面试6
题目链接 题目大意:根据先序遍历和中序遍历构造二叉树. 法一:DFS.根据模拟步骤,直接从先序和中序数组中找值然后加入二叉树中,即先从先序数组中确定根结点,然后再去中序数组中确定左子树和右子树的长度, ...
- grep常见操作整理(更新)
提取邮箱和URL [root@test88 ~]# cat url_email.txt root@gmail.com,http://blog.peter.com,peter@qq.com [root@ ...
- 查看linux 下进程运行时间(转)
原文地址:http://blog.csdn.net/tspangle/article/details/11731317 可通过ps 来查看,通过参数 -o 来查看 如: ps -eo pid,tty, ...
- hdu 1226(同余搜索)
超级密码 Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...