博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BBS论坛(一)
阅读量:6812 次
发布时间:2019-06-26

本文共 4990 字,大约阅读时间需要 16 分钟。

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

可以看到现在密码是加密保存的了

 

转载地址:http://bczzl.baihongyu.com/

你可能感兴趣的文章
我国特色国情下 智慧城市建设与国外有哪些区别
查看>>
MaxCompute帮助创业公司中减轻MySQL存储压力
查看>>
ASHRAE新标准促进降低数据中心能源消耗
查看>>
程序猿如何从产品的角度去提升应用的体验之Android权限优化篇
查看>>
夏普欲收回美洲品牌授权 海信总裁:严格按照合同办
查看>>
大数据市场迎来扩容期 本土内存数据库抢位崛起
查看>>
2016年亚太地区网络广告支出预计超过北美
查看>>
智慧医疗节约救治时间,降低患者死亡率
查看>>
zephir-(2)安装和初体验
查看>>
IPython4_Notebook
查看>>
IE7/IE8不兼容js trim函数,自己实现
查看>>
Objective-C的泛型
查看>>
Mac OSX 命令行知识[转]
查看>>
在Tornado中使用Django的ORM的注意事项
查看>>
用node-webkit把web应用打包成桌面应用
查看>>
ORM数据层框架的设计热点:更新指定的列的几种设计方案
查看>>
PHP+Sphinx建立高效的站内搜索引擎
查看>>
rac问题思考总结
查看>>
Linux计算命令
查看>>
deis客户端操作命令
查看>>