• 【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')

    目录结构:

  • 相关阅读:
    师弟大喜之日,送上一幅对联 求横批
    漫画:Google 走了
    产品研发流程改进
    Outlook2010 Bug 一则
    Android 手机用户版本比例
    CDMA 短信中心号码
    UIM卡 PIN 码特点
    [Accessibility] Missing contentDescription attribute on image
    java打印函数的调用堆栈
    android中解析Json
  • 原文地址:https://www.cnblogs.com/wanghong1994/p/13739182.html
Copyright © 2020-2023  润新知