点击排行展示
- 在请求根路由时去数据库查询按点击量排行的10条新闻
新闻new模型设计
class News(BaseModel, db.Model): """新闻""" __tablename__ = "info_news" id = db.Column(db.Integer, primary_key=True) # 新闻编号 title = db.Column(db.String(256), nullable=False) # 新闻标题 source = db.Column(db.String(64), nullable=False) # 新闻来源 digest = db.Column(db.String(512), nullable=False) # 新闻摘要 content = db.Column(db.Text, nullable=False) # 新闻内容 clicks = db.Column(db.Integer, default=0) # 浏览量 index_image_url = db.Column(db.String(256)) # 新闻列表图片路径 category_id = db.Column(db.Integer, db.ForeignKey("info_category.id")) user_id = db.Column(db.Integer, db.ForeignKey("info_user.id")) # 当前新闻的作者id status = db.Column(db.Integer, default=0) # 当前新闻状态 如果为0代表审核通过,1代表审核中,-1代表审核不通过 reason = db.Column(db.String(256)) # 未通过原因,status = -1 的时候使用 # 当前新闻的所有评论 comments = db.relationship("Comment", lazy="dynamic") def to_review_dict(self): resp_dict = { "id": self.id, "title": self.title, "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"), "status": self.status, "reason": self.reason if self.reason else "" } return resp_dict def to_basic_dict(self): resp_dict = { "id": self.id, "title": self.title, "source": self.source, "digest": self.digest, "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"), "index_image_url": self.index_image_url, "clicks": self.clicks, } return resp_dict def to_dict(self): resp_dict = { "id": self.id, "title": self.title, "source": self.source, "digest": self.digest, "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"), "content": self.content, "comments_count": self.comments.count(), "clicks": self.clicks, "category": self.category.to_dict(), "index_image_url": self.index_image_url, "author": self.user.to_dict() if self.user else None } return resp_dict
后端首页index视图代码
@index_blu.route('/') def index(): ... # 获取点击排行数据 news_list = None try: news_list = News.query.order_by(News.clicks.desc()).limit(constants.CLICK_RANK_MAX_NEWS) except Exception as e: current_app.logger.error(e) click_news_list = [] for news in news_list if news_list else []: click_news_list.append(news.to_basic_dict()) data = { "user_info": user.to_dict() if user else None, "click_news_list": click_news_list, } return render_template('news/index.html', data=data)
创建排行榜前三自定义过滤器
def do_class_index(index): if index == 0: return "first" elif index == 1: return "second" elif index == 2: return "third" else: return ""
- 在 app 创建的函数里面注册过滤器
from info.utils.common import do_index_class # 添加自定义过滤器 app.add_template_filter(do_index_class, "index_class")
前端index.html进行模板渲染,及通过对span标签使用自定义过滤器对排行前三特别显示
<div class="rank_con fr"> <div class="rank_title"> <h3>点击排行</h3> </div> <ul class="rank_list"> {% for news in data.click_news_list %} <li><span class="{{ loop.index0 | indexClass }}">{{ loop.index }}</span><a href="#">{{ news.title }}</a></li> {% endfor %} </ul> </div>
应用效果:
总结:
1、自定义模板过滤器,通过app.add_template_filter(函数名, '过滤器名称')进行注册, 过滤器名称为html使用的名称一致,例如上述例子
app.add_template_filter(do_index_class, "index_class") 与前端<li><span class="{{ loop.index0 | indexClass }}">{{ loop.index }}</span><a href="#">{{ news.title }}</a></li>
过滤器名称一致;
标签:index,Flask,新闻,self,db,dict,首页,news,id From: https://www.cnblogs.com/sds2023/p/18093033