1.1.项目结构搭建
(1)创建flask项目Perfect_bbs,然后搭建项目结构如下:
(2)构建蓝图
cms/views.py
# cmd/views.pyfrom flask import Blueprintbp = Blueprint("cms",__name__,url_prefix='/cms')@bp.route('/')def index(): return 'cms index'
cms/init.py
from .views import bp
common/views.py
# common/views.pyfrom flask import Blueprintbp = Blueprint("common",__name__,url_prefix='/common')@bp.route('/')def index(): return 'common index'
common/init.py
from .views import bp
front/views.py
# front/views.pyfrom flask import Blueprintbp = Blueprint("front",__name__)@bp.route('/')def index(): return 'front index'
front/init.py
from .views import bp
config.py
DEBUG = True
Perfect_bbs.py
# Perfect_bbs.pyfrom flask import Flaskfrom apps.cms import bp as cms_bpfrom apps.front import bp as front_bpfrom apps.common import bp as common_bpimport configapp = Flask(__name__)app.config.from_object(config)#注册蓝图app.register_blueprint(cms_bp)app.register_blueprint(front_bp)app.register_blueprint(common_bp)if __name__ == '__main__': app.run()
运行项目,浏览器访问
http://127.0.0.1:5000http://127.0.0.1:5000/cms/http://127.0.0.1:5000/common/
1.2.cms用户模型定义
建立CMS模型
(1)config.py
# config.py__author__ = 'derek'DEBUG = TrueDB_URI = "mysql+pymysql://root:123456@127.0.0.1:3306/bbs?charset=utf8"SQLALCHEMY_DATABASE_URI = DB_URISQLALCHEMY_TRACK_MODIFICATIONS =False
(2)exts.py
# exts.py__author__ = 'derek'from flask_sqlalchemy import SQLAlchemydb = SQLAlchemy()
(3)cms/models.py
# cms/models.py__author__ = 'derek'from exts import dbfrom datetime import datetimeclass CMSUser(db.Model): __tablename__='cms_user' id = db.Column(db.Integer,primary_key=True,autoincrement=True) username = db.Column(db.String(50),nullable=False) #不能为空 password = db.Column(db.String(100),nullable=False) email = db.Column(db.String(50),nullable=False,unique=True) #不能重复 join_time = db.Column(db.DateTime,default=datetime.now)
(4)manage.py
# manage.py__author__ = 'derek'from flask_script import Managerfrom flask_migrate import Migrate,MigrateCommandfrom Perfect_bbs import appfrom exts import dbfrom apps.cms import models as cms_modelsmanager = Manager(app)Migrate(app,db) #绑定app跟dbmanager.add_command('db',MigrateCommand)if __name__ == '__main__': manager.run()
(5)进cmd终端运行命令,生成到数据库
python manage.py db init # 初始化python manage.py db migrate #创建迁移脚本python manage.py db upgrade #生成到数据库
进数据库查看
项目下面也会多一个migrations目录
添加CMS用户
(1)Perfect_bbs.py
# Perfect_bbs.py__author__ = 'derek'from flask import Flaskfrom apps.cms import bp as cms_bpfrom apps.front import bp as front_bpfrom apps.common import bp as common_bpimport configfrom exts import dbdef create_app(): app = Flask(__name__) app.config.from_object(config) app.register_blueprint(cms_bp) app.register_blueprint(front_bp) app.register_blueprint(common_bp) db.init_app(app) return appif __name__ == '__main__': app = create_app() app.run()
(2)manage.py
# manage.py__author__ = 'derek'from flask_script import Managerfrom flask_migrate import Migrate,MigrateCommandfrom Perfect_bbs import create_appfrom exts import dbfrom apps.cms import models as cms_modelsCMSUser = cms_models.CMSUserapp = create_app()manager = Manager(app)Migrate(app,db) #绑定app跟dbmanager.add_command('db',MigrateCommand)@manager.option('-u','--username',dest='username')@manager.option('-p','--password',dest='password')@manager.option('-e','--email',dest='email')def create_cms_user(username,password,email): user = CMSUser(username=username,password=password,email=email) db.session.add(user) db.session.commit() print('cms用户添加成功')if __name__ == '__main__': manager.run()
(3)cmd终端添加用户
python manage.py create_cms_user -u derek -p 123456 -e 1184405959@qq.com
进数据库查看是否添加成功
发现密码是明文保存的,显然不行
密码加密
(1)cms/models.py
对外是‘passwor‘’显示,对内是”_password”
# cms/models.py__author__ = 'derek'from exts import dbfrom datetime import datetimefrom werkzeug.security import generate_password_hash,check_password_hashclass CMSUser(db.Model): __tablename__='cms_user' id = db.Column(db.Integer,primary_key=True,autoincrement=True) username = db.Column(db.String(50),nullable=False) #不能为空 _password = db.Column(db.String(100),nullable=False) email = db.Column(db.String(50),nullable=False,unique=True) #不能重复 join_time = db.Column(db.DateTime,default=datetime.now) def __init__(self,username,password,email): self.username = username self.password = password self.email = email @property def password(self): return self._password @password.setter def password(self,raw_password): self._password = generate_password_hash(raw_password) def check_password(self,raw_password): result = check_password_hash(self.password,raw_password) return result
(2)cmd终端重新添加用户
更改了字段之后要migrate和upgrade一下
python manage.py db migrate #创建迁移脚本python manage.py db upgrade #生成到数据库
添加用户
python manage.py create_cms_user -u derek -p 123456 -e 1184405959@qq.com
可以看到现在密码是加密保存的了