返回顶部
扩大
缩小

Zhang_derek

BBS论坛(三十二)

32.帖子排序功能完成

(1)front_index.html

<ul class="post-group-head">
                    {% if current_sort==1 %}
                        <li class="active"><a href="{{ url_for('front.index',bd=current_board) }}">最新</a></li>
                    {% else %}
                        <li><a href="{{ url_for('front.index',bd=current_board) }}">最新</a></li>
                    {% endif %}

                    {% if current_sort==2 %}
                        <li class="active"><a href="{{ url_for('front.index',st=2,bd=current_board) }}">精华帖子</a></li>
                    {% else %}
                        <li><a href="{{ url_for('front.index',st=2,bd=current_board) }}">精华帖子</a></li>
                    {% endif %}

                    {% if current_sort==3 %}
                        <li class="active"><a href="{{ url_for('front.index',st=3,bd=current_board) }}">点赞最多</a></li>
                    {% else %}
                        <li><a href="{{ url_for('front.index',st=3,bd=current_board) }}">点赞最多</a></li>
                    {% endif %}

                    {% if current_sort==4 %}
                        <li class="active"><a href="{{ url_for('front.index',st=4,bd=current_board) }}">评论最多</a></li>
                    {% else %}
                        <li><a href="{{ url_for('front.index',st=4,bd=current_board) }}">评论最多</a></li>
                    {% endif %}


                </ul>

                <ul class="post-list-group">
                    {% for post in posts %}
                        <li>
                            <div class="author-avatar-group">
                                <img src="{{ post.author.avatar or url_for('static',filename='common/images/logo.jpg') }}"
                                     alt="">
                            </div>

                            <div class="post-info-group">
                                <p class="post-title"><a href="{{ url_for('front.post_detail',post_id=post.id) }}">{{ post.title }}</a>
                                 {% if post.highlight %}
                                        <span class="label label-danger">精华帖</span>

                                 {% endif %}
                                </p>
                                <p class="post-info">
                                    <span>作者:{{ post.author.username }}</span>
                                    <span>发表时间:{{ post.create_time }}</span>
                                    <span>评论:0</span>
                                    <span>阅读数 :0</span>
                                </p>
                            </div>
                        </li>
                    {% endfor %}
                </ul>

(2)front/views.py

from sqlalchemy import func


@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)
    sort=request.args.get('st',type=int,default=1)

    banners = BannerModel.query.order_by(BannerModel.priority.desc()).limit(4)
    boards = BoardModel.query.all()
    start = (page - 1) * config.PER_PAGE
    end = start + config.PER_PAGE
    posts=None
    total=0
    query_obj=None
    if sort==1:
        query_obj=PostModel.query.order_by(PostModel.create_time.desc())
    elif sort==2:
        query_obj=db.session.query(PostModel).outerjoin(HighLight).order_by(HighLight.create_time.desc(),PostModel.create_time.desc())
    elif sort==3:
        query_obj=PostModel.query.order_by(PostModel.create_time.desc())
    elif sort==4:
        query_obj=db.session.query(PostModel).outerjoin(CommentModel).group_by(PostModel.id).order_by(func.count(CommentModel.id).desc(),PostModel.create_time.desc())

    if board_id:
        query_obj=query_obj.filter(PostModel.board_id==board_id)
        posts=query_obj.slice(start, end)
        total=query_obj.count()
    else:
        posts = query_obj.slice(start, end)
        total = query_obj.count()

    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,
        'current_sort':sort
    }
    return render_template('front/front_index.html', **context)

 

posted on 2018-06-11 23:28  zhang_derek  阅读(565)  评论(0编辑  收藏  举报

导航