session的定义:session是全局变量,用来暂时存储后台数据,能将一个函数中的数据传递给另一个函数。
@app.route('/login/time/',methods=['POST','GET'])
def user_time():
date1 = datetime.now().strftime('%Y-%m-%d')
session["date"] = date1
#print(type(date1))
now1= datetime.now().strftime('%H:%M:%S')
#print(now1)
session["now1"] = now1
db = pymysql.connect("localhost", "root", "", "test")
cursor = db.cursor()
cursor.execute("INSERT INTO work(username,dates,start)values (%s,%s,%s)",
(session["user_name"], date1, now1))
db.commit()
return render_template('user_time.html', now=now1)
@app.route('/login/time1/',methods=['POST','GET'])
def user_time1():
now2= datetime.now().strftime('%H:%M:%S')
print(type(now2))
#print(now2-session["now1"])
user_name=session["user_name"]
now1=session["now1"]
date=session["date"]
#print(date)
date=datetime.strptime(date,'%Y-%m-%d')
a=datetime.strptime(now1,'%H:%M:%S')
b=datetime.strptime(now2,'%H:%M:%S')
db = pymysql.connect("localhost", "root", "", "test")
cursor = db.cursor()
print(type(date))
print(type(b-a))
cursor.execute("UPDATE work SET off=%s , deltime=%s where username=%s and dates=%s",
((now2),(b-a),user_name,date))
db.commit()
return render_template('user_time1.html', now=now2,time=(b-a))