BBS论坛(二十八)

28.1.帖子分页功能完成

(1)manage.py

通过命令生成100个帖子,用于测试

@manager.command
def create_test_post():
    for x in range(1,100):
        title='我是标题%s'%x
        content='我是内容,我的编号是%s'%x
        board=BoardModel.query.first()
        author=FrontUser.query.first()
        post=PostModel(title=title,content=content)
        post.board=board
        post.author=author
        db.session.add(post)
        db.session.commit()
    print('测试帖添加成功')

(2)front/views.py

@bp.route('/')
def index():
    banners = BannerModel.query.order_by(BannerModel.priority.desc()).limit(4)
    boards = BoardModel.query.all()
    #获取当前页码数
    page = request.args.get(get_page_parameter(), type=int, default=1)
    # 显示10条帖子
    start = (page - 1) * config.PER_PAGE
    end = start + config.PER_PAGE
    posts = PostModel.query.slice(start, end)
    # bs_version=3:表示用Bootstrap v3版本
    pagination = Pagination(bs_version=3,page=page,total=PostModel.query.count(),
                            outer_window = 0, inner_window = 2)

    context = {
        'banners':banners,
        'boards':boards,
        'posts':posts,
        'pagination':pagination,
    }
    return render_template('front/front_index.html',**context)

(3)front_index/html

                <div style="text-align: center">
                    {{ pagination.links }}
                </div>

28.2.板块过滤功能完成

(1)front_index.html

 <div class="sm-container">
            <div style="margin-bottom: 10px;">
                <a class="btn btn-warning btn-block" href="{{ url_for('front.apost') }}" id="add-post-btn">发布帖子</a>
            </div>

            <div class="list-group">
                {% if current_board %}
                    <a href="/" class="list-group-item">所有版块</a>
                {% else %}
                    <a href="/" class="list-group-item active">所有版块</a>
                {% endif %}
                {% for board in boards %}
                    {% if board.id==current_board %}
                        <a href="{{ url_for('front.index',bd=board.id) }}"
                           class="list-group-item active">{{ board.name }}</a>
                    {% else %}
                        <a href="{{ url_for('front.index',bd=board.id) }}" class="list-group-item">{{ board.name }}</a>
                    {% endif %}
                {% endfor %}
            </div>
        </div>

(2)front/views.py

@bp.route('/')
def index():
    board_id = request.args.get('bd', type=int, default=None)
    # 获取当前页码数
    page = request.args.get(get_page_parameter(), type=int, default=1)
    banners = BannerModel.query.order_by(BannerModel.priority.desc()).limit(4)
    boards = BoardModel.query.all()
    # 显示10条帖子
    start = (page - 1) * config.PER_PAGE
    end = start + config.PER_PAGE
    posts = None
    total = 0
    if board_id:
        query_obj = PostModel.query.filter_by(board_id=board_id)
        posts = query_obj.slice(start,end)
        total = query_obj.count()
    else:
        posts = PostModel.query.slice(start, end)
        total = PostModel.query.count()
    # bs_version=3:表示用Bootstrap v3版本
    pagination = Pagination(bs_version=3,page=page,total=total,outer_window = 0, inner_window = 2)

    context = {
        'banners':banners,
        'boards':boards,
        'posts':posts,
        'pagination':pagination,
        'current_board':board_id      #把当前板块id传到前端,前端添加“active”样式
    }
    return render_template('front/front_index.html',**context)

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券