创建博客-完善关注功能(1)

在资料页中显示关注者

如果用户查看一个尚未关注用户的资料页,页面中要显示一个“Follow”(关注)按钮,如果查看已关注用户的资料则显示“Unfollow”(取消关注)按钮,并且,页面中最好能显示出关注者和被关注者的数量,再列出关注和被关注者的用户列表,并在相应的用户资料页中显示“Follow You”(关注了你)标志,对用户资料页模板的改动如下例所示:

# app/templates/user.html

#...

{% if current_user.can(Permission.FOLLOW) and user != current_user %}
          {% if not current_user.is_following(user) %}
          <a href="{{ url-for('.follow', username=user.username) }}"
            class="btn btn-primary">Follow</a>
          {% else %}
          <a href="{{ url_for'.unfollow', username = user.username) }}"
            class="btn btn-default">Unfollow
            </a>
          {% endif %}
        {% endif %}
        <a href="{{ url_for('.followers', username=user.username) }}">
          Followers:<span class='badge'>{{ user.followers.count() }}</span>
        </a>
        <a href="{{ url_for(.followed_by, username=user.username) }}">
          Following:<span class="badge">{{ user.followed.count() }}</span>
        </a>
        {% if current_user.is_authenticated() and user != current_user and user.is_following(current_user) %}
        | <span class="label label-default">Follows you</span>
        {% endif %}

这次修改模板用到了4个新端点,用户在其他用户的资料页中点击”Follow”(关注)按钮后,执行的是/follow/路由,这个路由的实现方法如下:

# app/main/views.py

#...

@main.route('/follow/<username>')
@login_required
@permission_required
def follow(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        flash('Invalid user.')
        return redirect(url_for('.index'))
    if current_user.is_following(user):
        flash('You are already following this user.')
        return redirect(url_for('.user',username=username))
    current_user.follow(user)
    flash('You are now following %s.' % username)
    return redirect(url_for('.user',username=username))

这个视图函数先加载请求的用户,确保用户存在且当前登录用户还没有关注这个用户,然后调用User模型中定义的辅助方法follow(),用以联接两个用户 /unfollow/<username>路由的实现方式类似

用户在其他用户的资料页中点击关注者数量后,将调用/followers/<username>路由,这个路由的实现如下所示:

# app/main/views.py
# ...
@main.route('/followers/<username>')
def followers(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        flash('Invalid user.')
        return redirect(url_for('.index'))
    page = request.args.get('page', 1, type=int)
    pagination = user.follower.paginate(
        page, per_page=current_app.config['FLASKY_FOLLOWERS_PER_PAGE'],
        error_out=False)
    follows = [{'user': item.follower, 'timestamp':item.timestamp}
           for item in pagination.items]

    return render_template('followers.html', user=user, title='Followers of',
                            endpoint = '.followers', pagination=pagination,
                            followers=followers)

这个函数加载并验证请求的用户,然后使用之前介绍的技术分页显示该用户的followers关系,由于查询关注者返回的是Follow实例列表,为了渲染方便,我们将其转换成一个新列表,列表中的各元素都包含usertimestamp字段

渲染关注者列表的模板可以写的通用一些,以便能用来渲染关注的用户列表和被关注的用户列表,模板接受的参数包括用户对象、分页链接使用的端点、分页对象和查询结果列表

followed_by端点的实现过程几乎一样,唯一的区别在于:用户列表从user.followed关系中获取,传入的模板的参数也要进行响应调整

followers.html模板使用两列表格实现,左边一列用于显示用户名和头像,右边一列用于显示Flask-Moment时间戳

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值