【FLASK】蓝图与session使用Redis缓存
manage.py:
from BlueMap.pro_flask import create_app app = create_app() if __name__ == '__main__': app.run()
settins.py
# debug模式 class DebugSetting(object): DEBUG = True SECRET_KEY = "debug" # 测试模式 class TesingSetting(object): DEBUG = True SECRET_KEY = "tesing"
__init__.py
from flask import Flask from redis import Redis from flask_session import Session from BlueMap.pro_flask.views import login,index from .settings import DebugSetting def create_app(): app = Flask(__name__) app.secret_key="1231321w13" app.config.from_object(DebugSetting) # 配置redis app.config['SESSION_TYPE'] = 'redis' # session类型为redis app.config['SESSION_REFRESH_EACH_REQUEST'] = True # session类型为redis app.config['SESSION_PERMANENT'] = False # 如果设置为True,则关闭浏览器session就失效。 app.config['SESSION_USE_SIGNER'] = False # 是否对发送到浏览器上session的cookie值进行加密 app.config['SESSION_KEY_PREFIX'] = 'session:' # 保存到session中的值的前缀 app.config['SESSION_REDIS'] = Redis(host='127.0.0.1',port=6379) Session(app) # 导入蓝图 app.register_blueprint(login.ps) app.register_blueprint(index.In) return app
login.py
from flask import Blueprint,render_template,session ps = Blueprint('proself',__name__) # 蓝图使用 @ps.route("/login",methods=['GET','POST']) def login(): session['username'] = "wanghong" return "登录成功"
index.py
from flask import Blueprint,session,render_template In = Blueprint("index",__name__) # 蓝图使用
@In.route("/index") def index(): return session.get('username')
目录结构:
Python全栈(后端、数据分析、脚本、爬虫、EXE客户端) / 前端(WEB,移动,H5) / Linux / SpringBoot / 机器学习