《2018年9月1日》【连续333天】
补充:
参考:https://cuiqingcai.com/5965.html
修改配置:
1.硬编码
2.字典
3.对象引入
4.文件导入;
Session: 存储用户回话:
from flask import Flask,session,request,render_template
import os
app = Flask(__name__) ## 实例化
# 配置
app.config.update(
DEBUG = True,
SECRET_KEY = os.urandom(24) # 使用os.urandom生成随机,进行加密
)
@app.route('/',methods=['GET','POST'])
def index():
# 判断是否是 GET
if request.method == 'GET':
return render_template("index.html",id=session.get('id')) # 用get,如果id不存在就不会返回异常,而是None
# 如果不是GET,就是 POST
# Linus: 好代码是很少使用if等判断的.
session['id'] = request.form.get('id')
return render_template('index.html',id=session.get('id'))
if __name__ == '__main__':
app.run()