BBS论坛(一)

时间:2024-01-20 21:22:41

BBS论坛(一)

1.1.项目结构搭建

(1)创建flask项目Perfect_bbs,然后搭建项目结构如下:

(2)构建蓝图

cms/views.py

# cmd/views.py

from flask import Blueprint

bp = 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.py

from flask import Blueprint

bp = 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.py

from flask import Blueprint

bp = 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.py

from flask import Flask
from apps.cms import bp as cms_bp
from apps.front import bp as front_bp
from apps.common import bp as common_bp
import config

app = 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:5000
http://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 = True

DB_URI = "mysql+pymysql://root:123456@127.0.0.1:3306/bbs?charset=utf8"

SQLALCHEMY_DATABASE_URI = DB_URI
SQLALCHEMY_TRACK_MODIFICATIONS =False

(2)exts.py

# exts.py
__author__ = 'derek'

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

(3)cms/models.py

# cms/models.py
__author__ = 'derek'

from exts import db
from datetime import datetime

class 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 Manager
from flask_migrate import Migrate,MigrateCommand
from Perfect_bbs import app
from exts import db
from apps.cms import models as cms_models

manager = Manager(app)

Migrate(app,db)   #绑定app跟db
manager.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 Flask
from apps.cms import bp as cms_bp
from apps.front import bp as front_bp
from apps.common import bp as common_bp
import config
from exts import db

def 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 app

if __name__ == '__main__':
    app = create_app()
    app.run()

(2)manage.py

# manage.py
__author__ = 'derek'

from flask_script import Manager
from flask_migrate import Migrate,MigrateCommand
from Perfect_bbs import create_app
from exts import db
from apps.cms import models as cms_models

CMSUser = cms_models.CMSUser
app = create_app()

manager = Manager(app)

Migrate(app,db)   #绑定app跟db
manager.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 db
from datetime import datetime
from werkzeug.security import generate_password_hash,check_password_hash

class 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

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

 

posted on 2018-06-05 23:43 zhang_derek 阅读(...) 评论(...) 编辑 收藏