第十二章 关注者(二)

在资料页面中显示关注者

1.在用户资料界面添加关注信息(app/templates/user.html)

...
<p>Member since {{ moment(user.member_since).format('L') }}. Last seen {{ moment(user.last_seen).fromNow() }}.</p>
    <p>{{ user.posts.count() }} blog posts</p>
    <p>
        {% 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() - 1 }}</span>
        </a>
        <a href="{{ url_for('.followed', username=user.username) }}">
            Following: <span class="badge">{{ user.followed.count() - 1 }}</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 %}
...

2.这次模板修改用到了4个新端点,在views.py中添加新增路由(app/main/views.py)

@main.route('/follow/<username>')
@login_required
@permission_required(Permission.FOLLOW)
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)
    db.session.commit()
    flash('You are now following %s.' % username)
    return redirect(url_for('.user', username=username))


@main.route('/unfollow/<username>')
@login_required
@permission_required(Permission.FOLLOW)
def unfollow(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        flash('Invalid user.')
        return redirect(url_for('.index'))
    if not current_user.is_following(user):
        flash('You are not following this user.')
        return redirect(url_for('.user', username=username))
    current_user.unfollow(user)
    db.session.commit()
    flash('You are not following %s anymore.' % username)
    return redirect(url_for('.user', username=username))


@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.followers.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,
                           follows=follows)


@main.route('/followed_by/<username>')
def followed_by(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.followed.paginate(
        page, per_page=current_app.config['FLASKY_FOLLOWERS_PER_PAGE'],
        error_out=False)
    follows = [{'user': item.followed, 'timestamp': item.timestamp}
               for item in pagination.items]
    return render_template('followers.html', user=user, title="Followed by",
                           endpoint='.followed_by', pagination=pagination,
                           follows=follows)

 注意:

  • 在上一节定义如下多对多关系时,关系的另一侧都是Follow模型,因此查询关注者和被关注者时,返回的都是Follow实例列表,为了渲染方便,我们使用列表推导式将其转换为一个新列表,新列表的各元素都包含User和Follow.timestamp字;
  • 渲染关注者和被关注者的模板均为follows.html,该模板使用2列表格实现,左侧显示用户名和头像,右侧显示时间:
  • templates/follows.html 
{% extends 'base.html' %}
{% import "_macros.html" as macros %}

{% block title %}Flasky - {{ title }} {{ user.username }}{% endblock %}

{% block page_content %}
    <div class="page-header"><h1>{{ title }} {{ user.username }}</h1></div>
    <table class="table table-hover follows">
        <thead>
            <tr>
                <th>User</th>
                <th>Since</th>
            </tr>
        </thead>
        {% for follow in follows %}
            {% if follow.user != user %}
                <tr>
                    <td>
                        <a href="{{ url_for('.user', username=follow.user.username) }}">
                            <img src="{{ follow.user.gravatar(size=32) }}" class="img-rounded">
                            {{ follow.user.username }}
                        </a>
                    </td>
                    <td>{{ moment(follow.timestamp).format('L') }}</td>
                </tr>
            {% endif %}
        {% endfor %}
    </table>

    <div class="pagination">
        {{ macros.pagination_widget(pagination, endpoint, username=user.username) }}
    </div>
{% endblock %}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值