【Flask项目】排行模块 # 11

排行展示:

排行相关:

后端Flask代码:

from . import blue_index
from flask import render_template, current_app, session, abort
from info.models import User, News
@blue_index.route("/")
def index():
    """
        首页相关
            点击排行
            1.从数据库获取排序后的6条新闻
            2.将数据返回到页面中
    :return:
    """
    # 获取点击排行前六条新闻
    news_clicks_list = []
    try:
        news_clicks_list = News.query.order_by(News.clicks.desc()).limit(6)
    except Exception as e:
        current_app.logger.error(e)
    context = {
        "user": user,
        "news_clicks_list": news_clicks_list,
    }

    # 3.返回信息到前端页面
    return render_template("index.html", context=context)

HTML代码: 

这里使用的是前后端不分离的写法:

<div class="rank_con fr">
    <div class="rank_title">
        <h3>点击排行</h3>
    </div>
    <ul class="rank_list">
        {% for new in context.news_clicks_list %}
            <li>
                <span class="{{ loop.index | do_rank }}">{{ loop.index }}</span>
                <a href="#">{{ new.title }}</a>
            </li>
        {% endfor %}
    </ul>
</div>

自定义过滤器:

在 info/utils 下新建文件 comment.py:

def do_rank(index):
    if index == 1:
        return "first"
    elif index == 2:
        return "second"
    elif index == 3:
        return "third"
    else:
        return ""

注册过滤器:

在info/__init__.py,函数create_app中:

def create_app(config_name):
    from info.utils.comment import do_rank
    app.add_template_filter(do_rank)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值