Flask–表单提交+cookie+session
from flask import Flask
from flask import request
from flask import render_template
from flask import make_response
from flask import session
app = Flask(name)
app.config[‘SECRET_KEY’] = ‘laowang’
@app.route(‘/’)
def index():
return ‘index’
@app.route(‘/demo’)
def demo():
return render_template(‘表单提交.html’)
@app.route(‘/add.post’,methods=[‘post’])
def demo1():
username = request.form.get(‘username’,None)
userpass = request.form.get(‘userpass’, None)
print(username,userpass) 打印结果,验证代码 可省略
respose = make_response()
respose.set_cookie(‘username’,username,max_age=600)第2个参数是表单输入的值,第3个参数是cookie的生命周期
respose.set_cookie(‘userpass’,userpass,max_age=600)
session[‘username’] = username 对其输入的用户名和密码进行加密
session[‘userpass’] = userpass
return ‘输入需要返回的数据’
@app.route(‘/demo2’,methods=[‘post’])此段函数只为验证上传图片保存本地 不做他用
def demo2():
pic = request.files.get(‘pic’)
pic.save(‘img/aaaa.png’)
return ‘demo2’
if name == ‘main‘:
app.run(debug=True)
前端HTML