flask中的session和g全局变量
#_*_ encoding: utf-8 _*_ @author: ty hery 2019/12/20
from flask import Flask, session, current_app, g
# g 是临时的本App里面的变量对象,通过对象的方法保存数据 如 g.username='张三',常用与一个请求之内的多个函数之间传递变量参数
# from werkzeug.routing import BaseConverter
import json
app = Flask(__name__)
app.config['SECRET_KEY'] = 'asdfsdafsdaf134561sdaf'
# flask的session必须设置秘钥字符串:SECRET_KEY,否则报错RuntimeError: The session is unavailable
# because no secret key was set. Set the secret_key on the application to something unique and secret.
# flask 默认把session保存到了cookie中
@app.route('/login',methods=['GET','POST'])
def login():
# 设置session数据
session['name'] = 'python'
session['mobile'] = '18611111111'
g.username = 'zhangsan'
say_hello()
return 'login success'
# @app.route('')
def say_hello():
username = g.username
print(g.username,'撒大防守打法')
pass
@app.route('/index')
def index():
# 获取session数据
name = session.get('name')
return 'hello {}'.format(name)
if __name__ == '__main__':
print('--哈哈01--',app.url_map,'--哈哈01--')
app.run(debug=True)
写入自己的博客中才能记得长久