BBS Forum (28)

BBS Forum (28)

28.1. Post paging function completed

(1) manage.py

Generate 100 posts by command for testing

@manager.command
def create_test_post():
    for x in range(1,100):
        title='I am the title %s'%x
        content='I am content, my number is %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('The test post was added successfully')

(2) front/views.py

@bp.route('/')
def index():
    banners = BannerModel.query.order_by(BannerModel.priority.desc()).limit(4)
    boards = BoardModel.query.all()
    #Get the current page number
    page = request.args.get(get_page_parameter(), type=int, default=1)
    # Show 10 posts
    start = (page-1) * config.PER_PAGE
    end = start + config.PER_PAGE
    posts = PostModel.query.slice(start, end)
    # bs_version=3: means using Bootstrap v3 version
    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. The plate filtering function is completed

(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">Post a post</a>
            </div>

            <div class="list-group">
                {% if current_board %}
                    <a href="/" class="list-group-item">All sections</a>
                {% else %}
                    <a href="/" class="list-group-item active">All sections</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)
    # Get the current page number
    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()
    # Show 10 posts
    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: means using Bootstrap v3 version
    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 #Pass the current board id to the front end, and add the "active" style to the front end
    }
    return render_template('front/front_index.html',**context)
Reference: https://cloud.tencent.com/developer/article/1390476 BBS Forum (28)-Cloud + Community-Tencent Cloud