主要工作:
前端后台数据库的连接,实现登录、更新密码和记录展示、查询学习echarts.js,实现个人数据分析实现结果如下:1.登录
读取表单值经路由提交后台,后台在数据库中查询学号对应的密码是否与输入一致,一致则可登录,否则提示后回到login界面 @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': # 判断是否是 POST 请求 # 获取表单数据 stuid = request.form.get('stuid') # 传入表单对应输入字段的 name 值 pwd = request.form.get('pwd') # 验证数据 if not stuid or not pwd: flash('') return redirect(url_for('login')) stu = Stu.query.filter(Stu.id==stuid).first() # 验证用户名和密码是否一致 if stu and stu.validate_password(pwd): return redirect(url_for('index')) # 重定向到主页 flash('Invalid username or password.') # 如果验证失败,显示错误消息 return redirect(url_for('login')) # 重定向回登录页面 return render_template('login.html') 使用cookie,方便保持、关闭登录状态 @app.route('/setcookie', methods = ['POST', 'GET']) def setcookie(): if request.method == 'POST': # 获取表单数据 stuid = request.form.get('stuid') # 传入表单对应输入字段的 name 值 pwd = request.form.get('pwd') # 验证数据 if not stuid or not pwd: flash('请输入正确的用户名和密码!')# 如果验证失败,显示错误消息 return redirect(url_for('login')) stu = Stu.query.filter(Stu.id==stuid).first() # 验证用户名和密码是否一致 if stu and stu.validate_password(pwd): resp = make_response(redirect(url_for('index'))) resp.set_cookie('stuid', stuid,max_age=3600) return resp # 重定向到主页 else: flash('请输入正确的用户名和密码!')# 如果验证失败,显示错误消息 return redirect(url_for('login')) 数据库存储的pwd为密码散列值 from werkzeug.security import generate_password_hash, check_password_hash pw_hash = generate_password_hash('dog') # 为密码 dog 生成密码散列值`个人数据分析
@app.route('/consume', methods = ['POST', 'GET']) def consume(): stuid=getcookie() time='全部' sql="select * "+"from consume "+"where id="+stuid+ " order by cdate,ctime desc" sql2='select term,round(sum(price),2) from consume where id='+stuid+' group by term order by term desc' sql3='select place,round(sum(price),2) from consume where id='+stuid+' group by place' if request.method == 'POST': dic={'第一学期':'1','第二学期':'2','第三学期':'3'} term = request.form['term'] time=term if(term=='全部'): return redirect(url_for('consume')) else: sql="select * "+"from consume "+"where id="+stuid+ " and term="+dic[term] sql3='select place,round(sum(price),2) from consume where id='+stuid+' and term='+dic[term]+' group by place' consumelist=[] # 执行SQL语句 cursor.execute(sql) # 获取所有记录列表 results = cursor.fetchall() for row in results: consume=Consume(row[0],row[1],row[2],row[3],row[4],row[5]) consumelist.append(consume) #每个学期占比 percent=[] cursor.execute(sql2) res2 = cursor.fetchall() for i in res2: percent.append(i[1]) #每个地点占比 cursor.execute(sql3) res3 = cursor.fetchall() percent2=res3 return render_template('consume_record.html',consumelist=consumelist,percent=percent,percent2=percent2,time=time)