31.帖子加精和取消加精功能完成

(1)apps/models.py

  1. class HighLight(db.Model):
  2. __tablename__='highlight_post'
  3. id = db.Column(db.Integer, primary_key=True, autoincrement=True)
  4. post_id=db.Column(db.Integer,db.ForeignKey('post.id'))
  5. create_time=db.Column(db.DateTime,default=datetime.now)
  6.  
  7. post=db.relationship('PostModel',backref='highlight')

(2)cms/views.py

  1. @bp.route('/posts/')
  2. @login_required
  3. @permission_required(CMSPermission.POSTER)
  4. def posts():
  5. context = {
  6. 'posts': PostModel.query.all()
  7. }
  8. return render_template('cms/cms_posts.html',**context)
  9.  
  10. @bp.route('/hpost/',methods=['POST'])
  11. @login_required
  12. @permission_required(CMSPermission.POSTER)
  13. def hpost():
  14. post_id=request.form.get('post_id')
  15. if not post_id:
  16. return restful.params_error(message='请传入帖子id')
  17. post=PostModel.query.get(post_id)
  18. if not post:
  19. return restful.params_error(message='没有这篇帖子')
  20. highlight=HighLight()
  21. highlight.post=post
  22. db.session.add(highlight)
  23. db.session.commit()
  24. return restful.success()
  25.  
  26. @bp.route('/uhpost/',methods=['POST'])
  27. @login_required
  28. @permission_required(CMSPermission.POSTER)
  29. def uhpost():
  30. post_id = request.form.get('post_id')
  31. if not post_id:
  32. return restful.params_error(message='请传入帖子id')
  33. post = PostModel.query.get(post_id)
  34. if not post:
  35. return restful.params_error(message='没有这篇帖子')
  36. print(post_id)
  37. highlight=HighLight.query.filter_by(post_id=post_id).first()
  38. print(highlight)
  39. db.session.delete(highlight)
  40. db.session.commit()
  41. return restful.success()

(3)cms_posts.html

  1. {% extends 'cms/cms_base.html' %}
  2. {% from 'common/_macros.html' import static %}
  3. {% block title %}
  4. 帖子管理
  5. {% endblock %}
  6.  
  7. {% block head %}
  8. <script src="{{ static('cms/js/posts.js') }}"></script>
  9. {% endblock %}
  10.  
  11. {% block page_title %}
  12. {{ self.title() }}
  13. {% endblock %}
  14.  
  15. {% block main_content %}
  16. <table class="table table-bordered">
  17. <thead>
  18. <tr>
  19. <th>标题</th>
  20. <th>发布时间</th>
  21. <th>版块</th>
  22. <th>作者</th>
  23. <th>操作</th>
  24. </tr>
  25. </thead>
  26. <tbody>
  27. {% for post in posts %}
  28. <tr data-id={{ post.id }} data-highlight={{ 1 if post.highlight else 0 }}>
  29. <td><a target="_blank" href="{{ url_for('front.post_detail',post_id=post.id) }}">{{ post.title }}</a>
  30. </td>
  31. <td>{{ post.create_time }}</td>
  32. <td>{{ post.board.name }}</td>
  33. <td>{{ post.author.username }}</td>
  34. <td>
  35. {% if post.highlight %}
  36. <button class="btn btn-info btn-xs highlight-btn">取消加精</button>
  37. {% else %}
  38. <button class="btn btn-danger btn-xs highlight-btn">加精</button>
  39. {% endif %}
  40. &nbsp;&nbsp;&nbsp;<button class="btn btn-default btn-xs">移除</button>
  41. </td>
  42. </tr>
  43. {% endfor %}
  44. </tbody>
  45. </table>
  46. {% endblock %}

(4)cms/js/posts.js

  1. $(function(){
  2. $('.highlight-btn').on('click',function(){
  3. var $this=$(this);
  4. var tr=$this.parent().parent();
  5. var post_id=tr.attr('data-id');
  6. var highlight=parseInt(tr.attr('data-highlight'));
  7. var url='';
  8. if(highlight){
  9. url='/cms/uhpost/'
  10. }else{
  11. url='/cms/hpost/'
  12. }
  13. zlajax.post({
  14. 'url':url,
  15. 'data':{
  16. 'post_id':post_id
  17. },
  18. 'success':function(data){
  19. if(data['code']==200){
  20. zlalert.alertSuccessToast('操作成功');
  21. setTimeout(function(){
  22. window.location.reload();
  23. },500);
  24. }else{
  25. zlalert.alertInfo(data['message']);
  26. }
  27. }
  28. })
  29. });
  30. });

BBS论坛(三十一)的更多相关文章

  1. BBS论坛(十一)

    11.1.前台用户模型创建 (1)apps/front/models.py 首先安装:pip install shortuuid class FrontUser(db.Model): __tablen ...

  2. python第一百三十天 ---简单的BBS论坛

    简单的BBS论坛 实现功能 git仓库地址:https://github.com/uge3/BBS 1.整体参考“抽屉新热榜” + “博客园” 2.实现不同论坛版块 3.帖子列表展示 4.个人博客主页 ...

  3. centos 邮件服务 腾讯企业邮箱(免费) 使用iRedmail 需要有公网的centos主机 发邮件协议:smtp 端口25 收邮件协议:pop3 端口110 iredmail安装配置 使用邮箱系统 第三十一节课

    centos   邮件服务  腾讯企业邮箱(免费) 使用iRedmail 需要有公网的centos主机 发邮件协议:smtp 端口25  收邮件协议:pop3 端口110  iredmail安装配置 ...

  4. Django项目 BBS论坛

    BBS论坛 一.项目表分析 二.自定义form组件 三.注册功能 四.BBS论坛 登录功能

  5. BBS论坛 注册功能

    三.注册功能 # views.py文件 def register(request): back_dic = {'code': 100, 'msg': ''} form_obj = myforms.My ...

  6. 【FastDev4Android框架开发】RecyclerView完全解析之下拉刷新与上拉加载SwipeRefreshLayout(三十一)

    转载请标明出处: http://blog.csdn.net/developer_jiangqq/article/details/49992269 本文出自:[江清清的博客] (一).前言: [好消息] ...

  7. Java,面试题,简历,Linux,大数据,常用开发工具类,API文档,电子书,各种思维导图资源,百度网盘资源,BBS论坛系统 ERP管理系统 OA办公自动化管理系统 车辆管理系统 各种后台管理系统

    Java,面试题,简历,Linux,大数据,常用开发工具类,API文档,电子书,各种思维导图资源,百度网盘资源BBS论坛系统 ERP管理系统 OA办公自动化管理系统 车辆管理系统 家庭理财系统 各种后 ...

  8. Python之路【第十八篇】Django小项目简单BBS论坛部分内容知识点

    开发一个简单的BBS论坛 项目需求: 整体参考“抽屉新热榜” + “虎嗅网” 实现不同论坛版块 帖子列表展示 帖子评论数.点赞数展示 在线用户展示 允许登录用户发贴.评论.点赞 允许上传文件 帖子可被 ...

  9. python 学习笔记二十 django项目bbs论坛

    项目:开发一个简单的BBS论坛 需求: 整体参考“抽屉新热榜” + “虎嗅网” 实现不同论坛版块 帖子列表展示 帖子评论数.点赞数展示 在线用户展示 允许登录用户发贴.评论.点赞 允许上传文件 帖子可 ...

  10. Bootstrap <基础三十一>插件概览

    在前面布局组件中所讨论到的组件仅仅是个开始.Bootstrap 自带 12 种 jQuery 插件,扩展了功能,可以给站点添加更多的互动.即使不是一名高级的 JavaScript 开发人员,也可以着手 ...

随机推荐

  1. 单机千万级MQTT连接服务器测试报告

    目标:测试创建1000万客户端连接到服务器端,服务器操作系统 Linux(任意一款发行版服务器版本).分别在两台硬件一样的服务器,其中一台用于服务器端运行,另一台用于创建千万客户端连接客户端机器.在硬 ...

  2. Solr的配置和在java中的使用

    Solr是一个全局站内搜索引擎,可以快速的搜索出结果. Solr依赖于tomcat,把Solr的war包放到tomcat中即可运行. 使用solr,需要在solr的schema.xml中配置solr与 ...

  3. iOS 9之后Url链接的NSUTF8StringEncoding转码实现

    在iOS中通过WebView加载Url或者请求HTTP时,若是链接中包含中文.特殊符号&%或是空格等都需要预先进行一下转码才可正常访问. 许久没编码,原先的方法已废弃了都,在此对应当前最新的方 ...

  4. 短网址API

    http://tao.tf/open/ API简介 API允许第三方自由调用URL缩短,基于text/json/jsonp/js模式,支持post.get提交. 支持缩短网址: 淘宝网(*.taoba ...

  5. 1.3 正则表达式和Python语言-1.3.5使用 search()在一个字符串中查找模式(搜索与匹配 的对比)

    1.3.5 使用 search()在一个字符串中查找模式(搜索与匹配的对比) 其实,想要搜索的模式出现在一个字符串中间部分的概率,远大于出现在字符串起始部分的概率.这也就是 search()派上用场的 ...

  6. BZOJ1431 : MLand

    考虑任意一棵生成树,它的代价是一个一次函数. 因此所有生成树的最小值随着时间变化呈现出的是一个上凸壳. 三分查找最大值即可. 时间复杂度$O(m\log m\log w)$. #include< ...

  7. MyBatis(10)使用association进行分步查询

    (1)接口中编写方法 public Emp getEmpByStep(Integer id); public Dept getDeptById(Integer id); (2)Mapper文件 < ...

  8. [LeetCode] Backspace String Compare 退格字符串比较

    Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...

  9. C++ struct结构体定义构造函数和析构函数,构造函数参数从VS2017平台转换到Qt5平台下构建出错,采用字符集转换函数将string类型转换为wstring,构建仍然出错!

    调试win硬件驱动,需要利用VS编译的win驱动构建自己的Qt5GUI程序: 其中部分win驱动源码如下 device_file::device_file(const std::string& ...

  10. 使用jQuery.form库中ajaxSubmit提交表单时遇到的一些问题

    初入前端,网上找的很多资料都不够详细,导致遇到很多问题,现记录如下: 1.首先引入 <script src="~/Scripts/jquery-1.10.2.js">& ...