Python实例题:基于 Flask 的博客系统

目录

Python实例题

题目

要求:

解题思路:

代码实现:

1. base.html

2. index.html

3. post.html

4. create_post.html

5. login.html

6. register.html

Python实例题

题目

基于 Flask 的博客系统

要求

  • 使用 Flask 框架构建一个简单的博客系统。
  • 实现用户认证(注册、登录、注销)。
  • 支持博客文章的创建、编辑、删除和查看。
  • 使用 SQLite 数据库存储用户和文章数据。
  • 添加评论功能,允许已登录用户对文章发表评论。
  • 实现简单的权限控制(如只有文章作者才能编辑 / 删除文章)。
  • 添加分页功能,美化界面(使用 Bootstrap)。

解题思路

  • 使用 Flask 蓝图组织代码结构。
  • 通过 Flask-Login 处理用户认证。
  • 使用 Flask-SQLAlchemy 管理数据库操作。
  • 结合 WTForms 实现表单验证。
  • 使用 Bootstrap 美化前端界面。

代码实现

from flask import Flask, render_template, request, redirect, url_for, flash, Blueprint
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import datetime
import os

# 初始化 Flask 应用
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your-secret-key-here'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(app.root_path, 'blog.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

# 初始化数据库
db = SQLAlchemy(app)

# 初始化 Flask-Login
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'auth.login'

# 数据模型
class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    password_hash = db.Column(db.String(120), nullable=False)
    posts = db.relationship('Post', backref='author', lazy=True)
    comments = db.relationship('Comment', backref='author', lazy=True)
    
    def set_password(self, password):
        self.password_hash = generate_password_hash(password)
    
    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    content = db.Column(db.Text, nullable=False)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    comments = db.relationship('Comment', backref='post', lazy=True)

class Comment(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.Text, nullable=False)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    post_id = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False)

# 用户加载回调
@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

# 认证蓝图
auth = Blueprint('auth', __name__)

@auth.route('/register', methods=['GET', 'POST'])
def register():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        
        if User.query.filter_by(username=username).first():
            flash('用户名已存在', 'error')
            return redirect(url_for('auth.register'))
        
        user = User(username=username)
        user.set_password(password)
        db.session.add(user)
        db.session.commit()
        
        flash('注册成功,请登录', 'success')
        return redirect(url_for('auth.login'))
    
    return render_template('register.html')

@auth.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        
        user = User.query.filter_by(username=username).first()
        
        if user and user.check_password(password):
            login_user(user)
            flash('登录成功', 'success')
            return redirect(url_for('main.index'))
        else:
            flash('用户名或密码错误', 'error')
    
    return render_template('login.html')

@auth.route('/logout')
@login_required
def logout():
    logout_user()
    flash('已注销', 'success')
    return redirect(url_for('main.index'))

# 主蓝图
main = Blueprint('main', __name__)

@main.route('/')
def index():
    page = request.args.get('page', 1, type=int)
    posts = Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5)
    return render_template('index.html', posts=posts)

@main.route('/post/new', methods=['GET', 'POST'])
@login_required
def new_post():
    if request.method == 'POST':
        title = request.form['title']
        content = request.form['content']
        
        post = Post(title=title, content=content, author=current_user)
        db.session.add(post)
        db.session.commit()
        
        flash('文章发布成功', 'success')
        return redirect(url_for('main.index'))
    
    return render_template('create_post.html', title='新文章')

@main.route('/post/')
def post(post_id):
    post = Post.query.get_or_404(post_id)
    return render_template('post.html', post=post)

@main.route('/post//update', methods=['GET', 'POST'])
@login_required
def update_post(post_id):
    post = Post.query.get_or_404(post_id)
    
    if post.author != current_user:
        flash('你无权编辑此文章', 'error')
        return redirect(url_for('main.post', post_id=post.id))
    
    if request.method == 'POST':
        post.title = request.form['title']
        post.content = request.form['content']
        db.session.commit()
        flash('文章已更新', 'success')
        return redirect(url_for('main.post', post_id=post.id))
    
    return render_template('create_post.html', title='更新文章', post=post)

@main.route('/post//delete', methods=['POST'])
@login_required
def delete_post(post_id):
    post = Post.query.get_or_404(post_id)
    
    if post.author != current_user:
        flash('你无权删除此文章', 'error')
        return redirect(url_for('main.post', post_id=post.id))
    
    db.session.delete(post)
    db.session.commit()
    flash('文章已删除', 'success')
    return redirect(url_for('main.index'))

@main.route('/post//comment', methods=['POST'])
@login_required
def add_comment(post_id):
    post = Post.query.get_or_404(post_id)
    content = request.form['content']
    
    comment = Comment(content=content, author=current_user, post=post)
    db.session.add(comment)
    db.session.commit()
    
    flash('评论已提交', 'success')
    return redirect(url_for('main.post', post_id=post_id))

# 注册蓝图
app.register_blueprint(main)
app.register_blueprint(auth)

# 创建数据库表
with app.app_context():
    db.create_all()

if __name__ == '__main__':
    app.run(debug=True)

对应的 HTML 模板文件(需要放在 templates 文件夹中):

1. base.html




    
    
    Flask博客
    


    
    
    
{% with messages = get_flashed_messages(with_categories=true) %} {% if messages %} {% for category, message in messages %}
{{ message }}
{% endfor %} {% endif %} {% endwith %} {% block content %}{% endblock %}

2. index.html

{% extends "base.html" %}

{% block content %}
    

博客文章

{% for post in posts.items %}

{{ post.title }}

{{ post.content[:200] }}{% if post.content|length > 200 %}...{% endif %}

阅读更多
{% endfor %} {% endblock %}

3. post.html

{% extends "base.html" %}

{% block content %}
    

{{ post.title }}

{{ post.content }}

评论 ({{ post.comments|length }})

{% for comment in post.comments %}

{{ comment.content }}

由 {{ comment.author.username }} 发布于 {{ comment.date_posted.strftime('%Y-%m-%d %H:%M') }}
{% endfor %} {% if current_user.is_authenticated %}
添加评论
{% else %}
登录 后可发表评论
{% endif %} {% endblock %}

4. create_post.html

{% extends "base.html" %}

{% block content %}
    

{{ title }}

{% endblock %}

5. login.html

{% extends "base.html" %}

{% block content %}
    

登录

还没有账号? 注册

{% endblock %}

6. register.html

{% extends "base.html" %}

{% block content %}
    

注册

已有账号? 登录

{% endblock %}

你可能感兴趣的:(实例,python,开发语言)