这是一个基于 Flask 的个人博客系统,用于实习考核与技术展示。
blog/
├── app/
│ ├── __init__.py # 应用工厂
│ ├── models.py # 数据模型(User / Post)
│ ├── posts/
│ │ ├── __init__.py
│ │ └── routes.py # 文章模块路由(CRUD)
│ ├── about/
│ │ ├── __init__.py
│ │ └── routes.py # 关于我模块
│ ├── templates/ # Jinja2 模板
│ │ ├── base.html
│ │ ├── index.html
│ │ ├── about.html
│ │ ├── post.html
│ │ ├── create.html
│ │ ├── edit.html
│ │ └── 404.html
│ └── static/
│ └── css/
│ └── style.css
├── migrations/ # 数据库迁移目录(预留)
├── config.py # 配置文件
├── requirements.txt # Python 依赖
├── run.py # 应用启动入口
└── README.md
git clone <repo-url>
cd blog
# Mac / Linux
python3 -m venv venv
source venv/bin/activate
# Windows
python -m venv venv
venv\Scripts\activate
pip install -r requirements.txt
python run.py
启动后自动完成以下操作:
blog.db)user 表和 post 表intern打开浏览器访问:http://127.0.0.1:5000
| 功能 | 路由 | 方法 | 说明 |
|---|---|---|---|
| 首页 | / |
GET | 展示最新文章列表 |
| 关于我 | /about |
GET | 个人简介页面 |
| 文章详情 | /post/<id> |
GET | 查看完整文章 |
| 写文章 | /create |
GET / POST | 创建新文章 |
| 编辑文章 | /edit/<id> |
GET / POST | 编辑已有文章 |
| 删除文章 | /delete/<id> |
POST | 删除文章 |
| 404 页面 | — | — | 自定义错误页 |
| 字段 | 类型 | 说明 |
|---|---|---|
| id | Integer (PK) | 用户 ID |
| username | String(50) | 用户名(唯一) |
| String(120) | 邮箱(唯一) | |
| password_hash | String(200) | 密码哈希 |
| 字段 | 类型 | 说明 |
|---|---|---|
| id | Integer (PK) | 文章 ID |
| title | String(200) | 文章标题 |
| content | Text | 文章内容 |
| created_at | DateTime | 创建时间 |
| updated_at | DateTime | 更新时间 |
| author_id | FK → user.id | 作者 ID |
本项目使用 Flask 应用工厂模式(create_app()),便于:
项目使用 Flask 蓝图进行模块化拆分:
posts_bp:文章相关路由about_bp:关于我页面# 查询所有文章(按时间倒序)
posts = Post.query.order_by(Post.created_at.desc()).all()
# 查询单篇文章(404 处理)
post = Post.query.get_or_404(post_id)
# 创建文章
post = Post(title="标题", content="内容", author_id=1)
db.session.add(post)
db.session.commit()
# 更新文章
post.title = "新标题"
db.session.commit()
# 删除文章
db.session.delete(post)
db.session.commit()
python
from app import create_app, db
from app.models import User, Post
app = create_app()
with app.app_context():
print("用户数:", User.query.count())
print("文章数:", Post.query.count())
blog.db 到 Git(已加入 .gitignore)SECRET_KEY(请在此处写下你在开发过程中遇到的难点及解决方案)
实习生 — 2026 年夏季实习项目