zoukankan      html  css  js  c++  java
  • 【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')

    目录结构:

  • 相关阅读:
    UNIX:处理SIGCHLD信号
    多维数组,指针数组使用,指向指针的指针
    bit field
    链表操作,获得泛型效果
    简明 Vim 练级攻略
    指针3,指向链表对象指针的指针
    大端模式,指针数组
    C宏设置掩码
    springboot 启动报错: Multiple Dockets with the same group name are not supported. The following duplicat
    HTML5学习笔记三
  • 原文地址:https://www.cnblogs.com/wanghong1994/p/13739182.html
Copyright © 2011-2022 走看看