Files
tech-blog/app/routes/frontend.py
hz4th_coder e689fed71e 初始化技术博客项目
功能特性:
- 前端文章展示 (Markdown渲染、代码高亮)
- 后台管理系统 (文章、分类、标签、作者管理)
- 图片上传 (点击、拖拽、粘贴)
- 网站设置 (Logo、底部宣传图片上传)
- 访问统计 (IP记录、趋势图表)
- 参考来源、附件支持
- 单端口部署 (16012)

技术栈:
- Flask 3.0 + SQLAlchemy
- SQLite数据库
- marked.js + highlight.js (Markdown渲染)
- Chart.js (统计图表)
2026-06-04 23:12:34 +08:00

131 lines
4.1 KiB
Python

"""前端路由"""
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/<slug>')
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/<int:category_id>')
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/<int:tag_id>')
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