该篇文章介绍了一个使用python语言编写的简单博客系统源码,该系统采用mvc架构,使用sqlalchemy进行数据库操作。数据库包含3个表:用户表、文章表和评论表。应用逻辑主要包含在models.py、routes.py和views.py文件中。用户界面包括登录页面、文章列表页面、文章详细页面、创建文章页面和评论文章页面。部署到web服务器使用以下命令:pip install -r requirements.txt,python app.py。
Python简单博客系统源码
引言
对于初学者来说,构建一个博客系统可以提升技术能力。本文提供了一个用Python编写的简单博客系统源码,供学习者参考。
系统架构
该系统采用MVC架构,将应用程序逻辑、数据和用户界面分开。
数据库设计
系统使用SQLAlchemy进行数据库操作。数据库包含以下表:
立即学习“Python免费学习笔记(深入)”;
- 用户表:存储用户登录信息和角色
- 文章表:存储文章标题、内容和作者
- 评论表:存储对文章的评论和评论者
应用程序逻辑
- models.py:定义数据库模型
- routes.py:定义应用程序路由和处理请求的函数
- views.py:提供用户界面模板
用户界面
- 登录页面:用户登录
- 文章列表页面:显示所有文章
- 文章详细页面:显示特定文章和评论
- 创建文章页面:创建新文章
- 评论文章页面:在文章上发表评论
部署
系统可以使用以下命令部署到Web服务器:
pip install -r requirements.txt
python app.py
源码
以下是最小可行代码(MVC):
models.py
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
username = Column(String(255), unique=True)
password = Column(String(255))
class Article(Base):
__tablename__ = 'articles'
id = Column(Integer, primary_key=True)
title = Column(String(255))
content = Column(String)
author_id = Column(Integer, ForeignKey('users.id'))
author = relationship("User", back_populates="articles")
class Comment(Base):
__tablename__ = 'comments'
id = Column(Integer, primary_key=True)
content = Column(String)
article_id = Column(Integer, ForeignKey('articles.id'))
author_id = Column(Integer, ForeignKey('users.id'))
author = relationship("User", back_populates="comments")
article = relationship("Article", back_populates="comments")
views.py
from flask import render_template, request, redirect, url_for, flash
from . import app
from .models import User, Article, Comment
from .forms import LoginForm, CreateArticleForm, CreateCommentForm
from flask_login import current_user, login_user, logout_user, login_required
@app.route('/')
def index():
articles = Article.query.all()
return render_template('index.html', articles=articles)
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user and user.check_password(form.password.data):
login_user(user)
return redirect(url_for('index'))
return render_template('login.html', form=form)
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
@app.route('/create-article', methods=['GET', 'POST'])
@login_required
def create_article():
form = CreateArticleForm()
if form.validate_on_submit():
article = Article(title=form.title.data, content=form.content.data, author=current_user)
db.session.add(article)
db.session.commit()
return redirect(url_for('index'))
return render_template('create_article.html', form=form)
@app.route('/article/<id>', methods=['GET', 'POST'])
def article(id):
article = Article.query.get_or_404(id)
form = CreateCommentForm()
if form.validate_on_submit():
comment = Comment(content=form.content.data, author=current_user, article=article)
db.session.add(comment)
db.session.commit()
return redirect(url_for('article', id=id))
return render_template('article.html', article=article, form=form)</id>