Flask博客系统 #
一、项目概述 #
1.1 功能需求 #
| 功能 | 说明 |
|---|---|
| 用户注册登录 | 用户认证系统 |
| 文章管理 | CRUD操作 |
| 评论功能 | 文章评论 |
| 分类标签 | 文章分类 |
| 搜索功能 | 文章搜索 |
1.2 项目结构 #
text
blog/
├── app/
│ ├── __init__.py
│ ├── models.py
│ ├── auth/
│ ├── main/
│ ├── templates/
│ └── static/
├── migrations/
├── config.py
├── requirements.txt
└── run.py
二、数据模型 #
python
from app import db
from datetime import datetime
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)
password_hash = db.Column(db.String(128))
posts = db.relationship('Post', backref='author', lazy=True)
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(200))
content = db.Column(db.Text)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
三、视图函数 #
python
from flask import render_template, redirect, url_for
from app.main import main
@main.route('/')
def index():
posts = Post.query.order_by(Post.created_at.desc()).all()
return render_template('index.html', posts=posts)
@main.route('/post/<int:id>')
def post(id):
post = Post.query.get_or_404(id)
return render_template('post.html', post=post)
四、下一步 #
接下来让我们学习 RESTful API项目,了解API开发!
最后更新:2026-03-28