使用Flask实现收藏功能和分页功能
2018-03-23 20:54
309 查看
Flask实现的功能:
user模型的更新(添加收藏功能)posts蓝图的建立
相关模板的更新
添加分页功能
目录结构:
模型user.py更新收藏功能
user.py:# 添加收藏功能 favorites = db.relationship('Posts', secondary='collections', backref=db.backref('users', lazy='dynamic'), lazy='dynamic') # 添加收藏 def add_favorite(self, pid): p = Posts.query.get(pid) self.favorites.append(p) # 取消收藏 def del_favorite(self, pid): p = Posts.query.get(pid) self.favorites.remove(p) # 判断是否已经收藏 def is_favorite(self, pid): # 获取所有收藏的博客 favorites = self.favorites.all() posts = list(filter(lambda p: p.id == pid, favorites)) if len(posts) > 0: return True return False
添加相应的posts蓝本:
views中posts.pyfrom flask import Blueprint, jsonify from flask_login import current_user posts = Blueprint('posts', __name__) @posts.route('/collect/<int:pid>') def collect(pid): # 判断是否收藏 if current_user.is_favorite(pid): # 取消收藏 current_user.del_favorite(pid) else: # 添加收藏 current_user.add_favorite(pid) # 将字典转为JSON字符串 return jsonify({'result': 'ok'})
在__init__.py添加posts蓝本:
from .main import main from .user import user from .posts import posts # 蓝本默认配置 DEFAULT_BLUEPRINT = ( # (蓝本, 前缀) (main, ''), (user, '/user'), (posts, '/posts'), )
修改index.html模板,增加post显示功能:
<div class="media-body"> <div style="float: right">发表于:{{ moment(p.timestamp).fromNow() }}</div> <h4 class="media-heading">{{ p.user.username }}</h4> {{ p.content }} {% if current_user.is_authenticated %} <div class="collect" url="{{ url_for('posts.collect', pid=p.id) }}" style="cursor: pointer;">{% if current_user.is_favorite(p.id) %}取消收藏{% else %}收藏{% endif %}</div> {% endif %} </div>
在index.html中增加ajax动态更新收藏按钮功能:
{% block scripts %} {{ super() }} <script type="text/javascript"> $(function () { $('.collect').click(function () { // this在回调函数中没有意义,需要临时保存 _this = this $.get($(this).attr('url'), function () { if ($(_this).text() == '收藏') { $(_this).text('取消收藏') } else { $(_this).text('收藏') } }) }) }) </script> {% endblock %}
添加分页功能:
mian.py视图函数更新分页功能:from flask import request @main.route('/', methods=['GET', 'POST']) def index(): 4000 form = PostsForm() if form.validate_on_submit(): if current_user.is_authenticated: u = current_user._get_current_object() # 创建对象 p = Posts(content=form.content.data, user=u) # 保存到数据库 db.session.add(p) flash('发表成功') return redirect(url_for('main.index')) else: flash('登录后才能发表') return redirect(url_for('user.login')) # 读取分页数据 page = request.args.get('page', 1, type=int) pagination = Posts.query.filter_by(rid=0).order_by(Posts.timestamp.desc()).paginate(page=page, per_page=5, error_out=False) posts = pagination.items return render_template('main/index.html', form=form, posts=posts, pagination=pagination)
添加common/macro.html模板,用于生成分页功能:
{% macro pagination_show(pagination, endpoint) %} <nav aria-label="Page navigation"> <ul class="pagination"> {# 上一页 #} <li {% if not pagination.has_prev %}class="disabled"{% endif %}> <a href="{% if pagination.has_prev %}{{ url_for(endpoint, page=pagination.prev_num, **kwargs) }}{% else %}#{% endif %}" aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> {# 中间页码 #} {% for p in pagination.iter_pages() %} {% if p %} <li {% if p==pagination.page %}class="active"{% endif %}><a href="{{ url_for(endpoint, page=p, **kwargs) }}">{{ p }}</a></li> {% else %} <li><a href="#">…</a></li> {% endif %} {% endfor %} {# 下一页 #} <li {% if not pagination.has_next %}class="disabled"{% endif %}> <a href="{% if pagination.has_next %}{{ url_for(endpoint, page=pagination.next_num, **kwargs) }}{% else %}#{% endif %}" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> </ul> </nav> {% endmacro %}
在index.html模板中添加相应功能:
{% from 'common/macro.html' import pagination_show %} {# 分页导航条 #} {{ pagination_show(pagination, 'main.index', id=3) }}
相关文章推荐
- 使用AspNetPager分页控件、分页存储过程及用户控件基类实现的完美分页功能
- 使用AspNetPager分页控件、分页存储过程及用户控件基类实现的完美分页功能
- 使用PagedDataSource类实现DataList和Repeater控件的分页显示功能
- 使用jsp-javabean-servlet实现分页功能
- 使用xml绑定实现分页功能
- 使用PagedDataSource类实现DataList和Repeater控件的分页显示功能
- 使用JQuery实现分页功能
- ORACLE三层嵌套实现分页功能和ROWNUM的使用
- ASP.NET使用AspNetPager实现简单的分页功能
- 模拟网易邮箱实现全选,全不的功能/使用DataList实现 加入购物车,编辑,删除,更新,取消功能。/试完成Datalist使用存储过程来分页
- 使用hibernate和struts2实现分页功能
- 使用Hibernate3实现分页功能
- Sencha Touch 2 中 listpaging 与 pullrefresh 2个插件的使用(分页功能实现)
- 使用.net FtpWebRequest 实现FTP常用功能收藏
- extremetable+hibernate实现分页 关于结合hibernate后台数据分页和eXtremeTable分页功能的使用
- 使用Ext的Grid,Form,Dialog来实现分页列表,创建,修改,删除功能
- 如何使用struts2结合MySQL实现分页功能?
- 使用Ext中的Grid,Form,Dialog来实现列表,创建,更新,删除,查找和分页功能
- 使用Hibernate3实现分页功能
- 使用PagedDataSource类实现DataList和Repeater控件的分页显示功能