flask小记

flask小记

一、flask最小项目

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
return '<h1>hello world<h1>'

app.run()

二、request

#requet 包含前端发送过来的所有请求数据
from flask import Flask,render_template,request

app = Flask(__name__)

@app.route('/index',methods=['GET','POST'])
def index():
if request.method == 'GET':
return render_template('index.html')
if request.method == 'POST':
name = request.form.get('name')
password = request.form.get('password')
print(name,password)
return 'this is post'


if __name__=='__main__':
app.run()

三、响应格式

from flask import Flask,make_response,json,jsonify
app =Flask(__name__)
app.config['JSON_AS_ASCII'] = False
@app.route('/index')
def index():
data = {
'name':'张三'
}

# response = make_response(json.dumps(data,ensure_ascii=False))

# response.mimetype = 'application/json'
# return response

return jsonify(data)#返回的数据为json类型

# return jsonify(json.dumps(data,ensure_ascii=False))

if __name__ == '__main__':
app.run()

四、路由

from flask import Flask
app = Flask(__name__)

@app.route('/hello',methods=['get','post'])
def hello():
return 'hello world'

@app.route('/hi',method=['post'])
def hi():
return 'hi hi'

@app.route('/user/<id>')
def index(id):
if id =='1':
return 'python'
if id ==str(2):
return 'django'
if int(id) ==3:
return 'flask'
return 'hello world'

#string 接受任何不包含斜杠的文本
#int 接受正整数
#float 接受正浮点数
#path 接受包含斜杠的文本

@app.route('/user/<int:id>')
def index(id):
if id == 1:
return 'python'
if id == 2:
return 'django'
if id == 3:
return 'flask'
return 'hello world'

@app.route('/hi',methods=['post'])
def hi():
return 'hi hi'

if __name__=='__main__':
app.run()

flask+python+mysql+navicat作业二

一、写一个会员信息表:包含的字段如图:

写接口:会员信息的增删改查

查找要求一个接口,要满足条件:1.可以显示所有人的

  1. 给固定参数条件的时候显示满足条件的信息80c566f412f7ffdd546444dda4b7bf4

二、写一个和会员信息表相关联的身体指数表

写接口:某个人身体指数信息的增加接口。满足条件:当这个人添了一个新的身体指数信息同时也要把会员信息表里的这个人的身体指数信息更新为最新的

身体指数查找接口:当我给某个人对应的参数时能查询到这个人身体历史指数信息581deacbab37525b99026a0b1eaccfe

代码

import pymysql
from flask import Flask,request,jsonify

app = Flask(__name__)

conn = pymysql.connect(host='127.0.0.1',port=3306,user='root',password='Yyzx150315_',charset='utf8',db='db2')
cursor=conn.cursor()

#会员注册
@app.route('/register',methods=['POST'])
def register():
id = request.form.get('id')
id_num = request.form.get('id_num')
name = request.form.get('name')
height = request.form.get('height')
weight = request.form.get('weight')
body_fat = request.form.get('body_fat')
bmi = request.form.get('bmi')
fitness_station = request.form.get('fitness_station')
age = request.form.get('age')
gender = request.form.get('gender')
phone_number = request.form.get('phone_number')
image = request.form.get('image')
create_time = request.form.get('create_time')
update_time = request.form.get('update_time')

cursor.execute("insert into ms_information(id,id_num,name,height,weight,body_fat,bmi,fitness_station,age,gender,phone_number,image,create_time,update_time) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",(id,id_num,name,height,weight,body_fat,bmi,fitness_station,age,gender,phone_number,image,create_time,update_time))
conn.commit()

return jsonify({"code":200,"msg":'success'})
#会员注销
@app.route('/delete',methods=['POST'])
def delete():
id_num = request.form.get('id_num')

cursor.execute("delete * from ms_information where id_num = %s",(id_num,))
return jsonify({"code": 200, "msg": 'success'})
#会员信息更改
@app.route('/update',methods=['POST'])
def update():
id = request.form.get('id')
id_num = request.form.get('id_num')
name = request.form.get('name')
height = request.form.get('height')
weight = request.form.get('weight')
body_fat = request.form.get('body_fat')
bmi = request.form.get('bmi')
fitness_station = request.form.get('fitness_station')
age = request.form.get('age')
gender = request.form.get('gender')
phone_number = request.form.get('phone_number')
image = request.form.get('image')
create_time = request.form.get('create_time')
update_time = request.form.get('update_time')

cursor.execute(
"update ms_information set id =%s,id_num =%s,name =%s,height =%s,weight =%s,body_fat =%s,bmi =%s,fitness_station =%s,age =%s,gender =%s,phone_number =%s,image =%s,create_time =%s,update_time =%s",
(
id, id_num, name, height, weight, body_fat, bmi, fitness_station, age, gender, phone_number, image, create_time,
update_time))
conn.commit()

return jsonify({"code": 200, "msg": 'success'})

#会员信息查询
@app.route('/find_user', methods=['GET'])
def find_user():
id_num =request.form.get('id_num')
cursor.execute("select * from ms_information where id_num = %s",(id_num))
user = cursor.fetchone()
if user:
return jsonify({"code":200,"msg":'success'})
else:
return jsonify({"code":400,"msg":'success'})
#可以查出所有人信息
@app.route('/allfind_user', methods=['GET'])
def allfind_user():
cursor.execute("select * from ms_information")
user = cursor.fetchone()
if user:
return jsonify({"code":200,"msg":'success'})
else:
return jsonify({"code":400,"msg":'success'})

#按固定条件查询信息
#身份证号码
@app.route('/find_id_num', methods=['GET'])
def find_id_num():
id_num =request.form.get('id_num')
cursor.execute("select * from ms_information where id_num = %s",(id_num))
user = cursor.fetchone()
if user:
return jsonify({"code":200,"msg":'success'})
else:
return jsonify({"code":400,"msg":'success'})
#id
@app.route('/find_id', methods=['GET'])
def find_id():
id = request.form.get('id')
cursor.execute("select * from ms_information where id = %s",(id))
user = cursor.fetchone()
if user:
return jsonify({"code":200,"msg":'success'})
else:
return jsonify({"code":400,"msg":'success'})
#按姓名查
@app.route('/find_name', methods=['GET'])
def find_name():
name = request.form.get('name')
cursor.execute("select * from ms_information where name = %s",(name))
user = cursor.fetchone()
if user:
return jsonify({"code":200,"msg":'success'})
else:
return jsonify({"code":400,"msg":'success'})

#按身高查
@app.route('/find_height', methods=['GET'])
def find_height():
height = request.form.get('height')
cursor.execute("select * from ms_information where height = %s",(height))
user = cursor.fetchone()
if user:
return jsonify({"code":200,"msg":'success'})
else:
return jsonify({"code":400,"msg":'success'})
#按体重查
@app.route('/find_weight', methods=['GET'])
def find_weight():
weight = request.form.get('weight')
cursor.execute("select * from ms_information where weight = %s",(weight))
user = cursor.fetchone()
if user:
return jsonify({"code":200,"msg":'success'})
else:
return jsonify({"code":400,"msg":'success'})
#按体脂率查
@app.route('/find_body_fat', methods=['GET'])
def find_body_fat():
body_fat = request.form.get('body_fat')
cursor.execute("select * from ms_information where body_fat = %s",(body_fat))
user = cursor.fetchone()
if user:
return jsonify({"code":200,"msg":'success'})
else:
return jsonify({"code":400,"msg":'success'})
#按bmi查
@app.route('/find_bmi', methods=['GET'])
def singlefind_user():
bmi = request.form.get('bmi')
cursor.execute("select * from ms_information where bmi = %s",(bmi))
user = cursor.fetchone()
if user:
return jsonify({"code":200,"msg":'success'})
else:
return jsonify({"code":400,"msg":'success'})

#按所属健身站查
@app.route('/find_fitness_station', methods=['GET'])
def find_fitness_station():
fitness_station = request.form.get('fitness_station')
cursor.execute("select * from ms_information where fitness_station = %s",(fitness_station))
user = cursor.fetchone()
if user:
return jsonify({"code":200,"msg":'success'})
else:
return jsonify({"code":400,"msg":'success'})
#按年龄查
@app.route('/find_age', methods=['GET'])
def find_age():
age = request.form.get('age')
cursor.execute("select * from ms_information where age = %s",(age))
user = cursor.fetchone()
if user:
return jsonify({"code":200,"msg":'success'})
else:
return jsonify({"code":400,"msg":'success'})
#按性别查
@app.route('/find_gender', methods=['GET'])
def find_gender():
gender = request.form.get('gender')
cursor.execute("select * from ms_information where gender = %s",(gender))
user = cursor.fetchone()
if user:
return jsonify({"code":200,"msg":'success'})
else:
return jsonify({"code":400,"msg":'success'})

#按手机号码查
@app.route('/find_phone_number', methods=['GET'])
def find_phone_number():
phone_number = request.form.get('phone_number')
cursor.execute("select * from ms_information where phone_number = %s",(phone_number))
user = cursor.fetchone()
if user:
return jsonify({"code":200,"msg":'success'})
else:
return jsonify({"code":400,"msg":'success'})

#按人脸图像查
@app.route('/find_image', methods=['GET'])
def find_image():
image = request.form.get('image')
cursor.execute("select * from ms_information where image = %s",(image))
user = cursor.fetchone()
if user:
return jsonify({"code":200,"msg":'success'})
else:
return jsonify({"code":400,"msg":'success'})

#按创建时间查
@app.route('/find_create_time', methods=['GET'])
def find_create_time():
create_time = request.form.get('create_time')
cursor.execute("select * from ms_information where create_time = %s",(create_time))
user = cursor.fetchone()
if user:
return jsonify({"code":200,"msg":'success'})
else:
return jsonify({"code":400,"msg":'success'})

#按更新时间查
@app.route('/find_update_time', methods=['GET'])
def find_update_time():
update_time = request.form.get('update_time')
cursor.execute("select * from ms_information where update_time = %s",(update_time))
user = cursor.fetchone()
if user:
return jsonify({"code":200,"msg":'success'})
else:
return jsonify({"code":400,"msg":'success'})

#个人信息更改
@app.route('/person_update',methods=['POST'])
def person_update():
id = request.form.get('id')
id_num = request.form.get('id_num')
name = request.form.get('name')
height = request.form.get('height')
weight = request.form.get('weight')
body_fat = request.form.get('body_fat')
bmi = request.form.get('bmi')
fitness_station = request.form.get('fitness_station')
age = request.form.get('age')
gender = request.form.get('gender')
phone_number = request.form.get('phone_number')
image = request.form.get('image')
create_time = request.form.get('create_time')
update_time = request.form.get('update_time')

cursor.execute("update physical_fitness set update_time =%s,height =%s,weight =%s,body_fat =%s,bmi =%s",
(update_time, height, weight, body_fat, bmi))
cursor.execute(
"update ms_information set id =%s,id_num =%s,name =%s,height =%s,weight =%s,body_fat =%s,bmi =%s,fitness_station =%s,age =%s,gender =%s,phone_number =%s,image =%s,create_time =%s,update_time =%s",
(
id, id_num, name, height, weight, body_fat, bmi, fitness_station, age, gender, phone_number, image, create_time,
update_time))

conn.commit()

return jsonify({"code": 200, "msg": 'success'})


#查询用户身体素质信息表中的所有内容
@app.route('/personall_information', methods=['GET'])
def personall_information():
cursor.execute("select * from ms_information")
user = cursor.fetchone()
if user:
return jsonify({"code":200,"msg":'success'})
else:
return jsonify({"code":400,"msg":'success'})

if __name__ == '__main__':
app.run()