"""前端路由""" from flask import Blueprint, render_template, request, abort from app.services import ArticleService, CategoryService, TagService frontend_bp = Blueprint('frontend', __name__) @frontend_bp.route('/') def index(): """首页""" page = request.args.get('page', 1, type=int) category_id = request.args.get('category', type=int) tag_id = request.args.get('tag', type=int) # 获取文章列表 articles = ArticleService.get_published_list( page=page, category_id=category_id, tag_id=tag_id ) # 获取侧边栏数据 categories = CategoryService.get_all() tags = TagService.get_all() hot_articles = ArticleService.get_hot_articles() recent_articles = ArticleService.get_recent_articles() return render_template('frontend/index.html', articles=articles, categories=categories, tags=tags, hot_articles=hot_articles, recent_articles=recent_articles, current_category=category_id, current_tag=tag_id) @frontend_bp.route('/article/') def article(slug): """文章详情页""" from flask import g article = ArticleService.get_by_slug(slug) if not article: abort(404) # 保存文章ID用于访问统计 g.current_article_id = article.id # 获取侧边栏数据 categories = CategoryService.get_all() tags = TagService.get_all() hot_articles = ArticleService.get_hot_articles() recent_articles = ArticleService.get_recent_articles() return render_template('frontend/article.html', article=article, categories=categories, tags=tags, hot_articles=hot_articles, recent_articles=recent_articles) @frontend_bp.route('/category/') def category(category_id): """分类文章列表""" category_obj = CategoryService.get_by_id(category_id) if not category_obj: abort(404) page = request.args.get('page', 1, type=int) articles = ArticleService.get_published_list( page=page, category_id=category_id ) # 获取侧边栏数据 categories = CategoryService.get_all() tags = TagService.get_all() hot_articles = ArticleService.get_hot_articles() recent_articles = ArticleService.get_recent_articles() return render_template('frontend/index.html', articles=articles, categories=categories, tags=tags, hot_articles=hot_articles, recent_articles=recent_articles, current_category=category_id, category_title=category_obj.name) @frontend_bp.route('/tag/') def tag(tag_id): """标签文章列表""" tag_obj = TagService.get_by_id(tag_id) if not tag_obj: abort(404) page = request.args.get('page', 1, type=int) articles = ArticleService.get_published_list( page=page, tag_id=tag_id ) # 获取侧边栏数据 categories = CategoryService.get_all() tags = TagService.get_all() hot_articles = ArticleService.get_hot_articles() recent_articles = ArticleService.get_recent_articles() return render_template('frontend/index.html', articles=articles, categories=categories, tags=tags, hot_articles=hot_articles, recent_articles=recent_articles, current_tag=tag_id, tag_title=tag_obj.name) @frontend_bp.app_errorhandler(404) def page_not_found(e): """404页面""" return render_template('frontend/404.html'), 404 @frontend_bp.app_errorhandler(500) def internal_error(e): """500页面""" return render_template('frontend/500.html'), 500