浏览目录
flask-session
安装
1
|
pip3 install flask - session |
使用步骤
__init__.py
导入组件
1
|
from flask_session import Session |
实例化session,传入app
1
2
3
4
5
6
|
def create_app(): app = Flask(__name__) app.config.from_object( 'settings.ProConfig' ) # Flask-Session: 第一步示例Session Session(app) return app |
加入配置文件
1
2
3
|
# Flask-Session: 第二步配置 # SESSION_TYPE = 'redis' # SESSION_REDIS = Redis(host='192.168.0.94', port='6379') |
现在就可以将请求结束后保存在浏览器cookie中的session保存在redis中了。
flask-sqlalchemy
安装
1
|
pip3 install flask - sqlalchemy |
使用步骤
__init__.py
1
2
3
|
#导入并实例化SQLAlchemy from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() |
注意事项:
- 必须在导入蓝图之前
- 必须导入models.py
初始化
1
|
db.init_app(app) #app里面有所有的配置文件 |
在配置文件中写入配置文件
1
2
3
4
|
# ##### SQLALchemy配置文件 ##### SQLALCHEMY_DATABASE_URI = "mysql+pymysql://root:123456@127.0.0.1:3306/mysql_test?charset=utf8" SQLALCHEMY_POOL_SIZE = 10 SQLALCHEMY_MAX_OVERFLOW = 5 |
创建models.py中的类
1
2
3
4
5
6
7
8
9
10
11
12
13
|
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column from sqlalchemy import Integer,String,Text,Date,DateTime from sqlalchemy import create_engine from chun import db class Users(db.Model): __tablename__ = 'users' id = Column(Integer, primary_key = True ) name = Column(String( 32 ), index = True , nullable = False ) depart_id = Column(Integer) |
生成表(使用app上下文)
1
2
3
4
5
6
7
8
9
10
11
|
from blueprint_test import db,create_app app = create_app() app_ctx = app.app_context() # app_ctx = app/g with app_ctx: # __enter__,通过LocalStack放入Local中 db.create_all() # 调用LocalStack放入Local中获取app,再去app中获取配置 #__exit__ 通过localstack的pop方法将local中的数据移除 # with obj: #先触发__enter__方法,之后执行with缩进后的内容,结束后触发__exit__方法 # pass |
基于orm对数据进行操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
from flask import Blueprint from blueprint_test import db from blueprint_test import models us = Blueprint( "us" ,__name__) @us .route( "/index" ) def index(): # 使用SQLAlchemy在数据库中插入一条数据 # db.session.add(models.Users(name='高件套',depart_id=1)) # db.session.commit() # db.session.remove() result = db.session.query(models.Users). all () print (result) db.session.remove() return 'Index' |
现在你会发现,数据库已经被你掌控了。
flask-script
安装
1
|
pip3 install flask - script |
功能
增加runserver
1
2
3
4
5
6
7
8
9
10
|
from chun import create_app from flask_script import Manager app = create_app() manager = Manager(app) if __name__ = = '__main__' : # app.run() manager.run() |
可以自定义ip和端口
位置传参
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
from chun import create_app from flask_script import Manager app = create_app() manager = Manager(app) @manager .command def custom(arg): """ 自定义命令 python manage.py custom 123 :param arg: :return: """ print (arg) if __name__ = = '__main__' : # app.run() manager.run() |
关键字传参
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
from chun import create_app from flask_script import Manager app = create_app() manager = Manager(app) @manager .option( '-n' , '--name' , dest = 'name' ) @manager .option( '-u' , '--url' , dest = 'url' ) def cmd(name, url): """ 自定义命令 执行: python manage.py cmd -n wupeiqi -u http://www.oldboyedu.com :param name: :param url: :return: """ print (name, url) if __name__ = = '__main__' : # app.run() manager.run() |
flask-migrate
安装
1
|
pip3 install flask - migrate |
依赖:flask-script,要想用flask-migtare,必须在flask-script的基础上。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
#!/usr/bin/env python # -*- coding:utf-8 -*- from sansa import create_app from sansa import db from flask_script import Manager from flask_migrate import Migrate, MigrateCommand #1、先导入 app = create_app() manager = Manager(app) Migrate(app, db) #2 """ # 数据库迁移命名 python manage.py db init #只有第一次执行初始化 python manage.py db migrate # 相当于Django的makemirations python manage.py db upgrade # 相当于Django的migrate """ manager.add_command( 'db' , MigrateCommand) #3 if __name__ = = '__main__' : manager.run() # app.run() |