这个项目刚开始是跟着哔哩哔哩上的一个教程学习的,后面完成初步实现后,我按照自己的设计加入了新的功能。
用户表(user)
问题表(question)
回答表(answer)
回复表(reply)
邮箱验证码表(email_captcha)
最热文章列表按照文章浏览数量从高到低排列,点击文章标题,进入文章详情页,文章浏览数量+1。
问题内容为Markdown格式,就和写博客这个格式一样,目前我没有加入上传图片的功能。
不能删除别人的评论和回复,这个点击删除会显示错误,这里的删除按钮我后面再去除掉。点击文章标题就可以跳转到别人评论你的文章。
对于自己发布的文章提供编辑和删除功能,删除后文章不复存在,对应的评论和回复一并删除。评论数量和浏览数量也同时删除。
删除自己发布的评论后,相对应的回复一并删除,同时相对应的文章减去删除的评论和回复的数量。
alembic==1.10.3
blinker==1.6.2
click==8.1.3
colorama==0.4.6
dnspython==2.3.0
email-validator==1.3.1
Flask==2.2.3
Flask-Admin==1.6.1
Flask-Mail==0.9.1
Flask-Migrate==4.0.4
Flask-SQLAlchemy==3.0.3
Flask-WTF==1.1.1
greenlet==2.0.2
idna==3.4
itsdangerous==2.1.2
Jinja2==3.1.2
Mako==1.2.4
MarkupSafe==2.1.2
mistune==2.0.5
PyMySQL==1.0.3
SQLAlchemy==2.0.9
typing_extensions==4.5.0
Werkzeug==2.2.3
WTForms==3.0.1
from flask import Flask, session, g
import config
from exts import db, mail, admin,compress
from models import UserModel
from blueprints.qa import bp as qa_bp
from blueprints.auth import bp as auth_bp
from blueprints.manage import bp as manage_bp
from flask_migrate import Migrate
app = Flask(__name__)
# 绑定配置文件
app.config.from_object(config)
migrate = Migrate(app, db)
'''
迁移三部曲flask db init(初始执行一次即可);flask db migrate;flask db upgrade;
'''
compress.init_app(app)
db.init_app(app)
mail.init_app(app)
admin.init_app(app)
app.register_blueprint(qa_bp)
app.register_blueprint(auth_bp)
app.register_blueprint(manage_bp)
@app.before_request
def before_request():
user_id = session.get('user_id')
if user_id:
user = UserModel.query.filter_by(id=user_id).first()
setattr(g, "user", user)
else:
setattr(g, "user", None)
@app.context_processor
def context_processor():
return {"user": g.user}
if __name__ == '__main__':
app.run(debug=True)
from exts import db, admin
from datetime import datetime
class UserModel(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String(100), nullable=False)
password = db.Column(db.String(200), nullable=False)
email = db.Column(db.String(100), nullable=False, unique=True)
join_time = db.Column(db.DateTime, default=datetime.now)
class EmailCaptchaModel(db.Model):
__tablename__ = 'email_captcha'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
email = db.Column(db.String(100), nullable=False)
captcha = db.Column(db.String(100), nullable=False)
class QuestionModel(db.Model):
__tablename__ = 'question'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String(100), nullable=False, index=True) # 添加索引
content = db.Column(db.Text, nullable=False)
content_html = db.Column(db.Text, nullable=False)
create_time = db.Column(db.DateTime, default=datetime.now, index=True) # 添加索引
author_id = db.Column(db.Integer, db.ForeignKey("user.id"), index=True) # 添加索引
author = db.relationship(UserModel, backref="question")
class AnswerModel(db.Model):
__tablename__ = 'answer'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
content = db.Column(db.Text, nullable=False)
create_time = db.Column(db.DateTime, default=datetime.now, index=True) # 添加索引
question_id = db.Column(db.Integer, db.ForeignKey("question.id"), index=True) # 添加索引
author_id = db.Column(db.Integer, db.ForeignKey("user.id"), index=True) # 添加索引
question = db.relationship(QuestionModel, backref=db.backref('answers', order_by=create_time.desc()))
author = db.relationship(UserModel, backref="answers")
class ReplyModel(db.Model):
__tablename__ = 'reply'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
content = db.Column(db.Text, nullable=False)
question_id = db.Column(db.Integer, db.ForeignKey("question.id"), index=True) # 添加索引
answer_id = db.Column(db.Integer, db.ForeignKey("answer.id"), index=True) # 添加索引
author_id = db.Column(db.Integer, db.ForeignKey("user.id"), index=True) # 添加索引
create_time = db.Column(db.DateTime, default=datetime.now, index=True) # 添加索引
answer = db.relationship(AnswerModel, backref=db.backref('replys', order_by=create_time.desc()))
question = db.relationship(QuestionModel, backref=db.backref('replys', order_by=create_time.desc()))
author = db.relationship(UserModel, backref="replys")
from flask_admin.contrib.sqla import ModelView
class UserView(ModelView):
column_list = ['id', 'username', 'email', 'join_time']
admin.add_view(UserView(UserModel, db.session))
admin.add_view(ModelView(QuestionModel, db.session))
大多数代码已经省略(这全部粘贴出来恐怕得几万字)这个项目还有很多不完善的地方,后面有时间我再完善。真是 书到用时方恨少。