需要安装的库
安装Flask pip install Flask
安装pip install flask_restful
完整代码如下
'''
实现学生信息管理
1,登录学生信息管理
2,查询所有学生信息
3,增加学生信息
4,删除学生信息
5,修改学生信息
'''
from flask import Flask,jsonify
from flask_restful import Api,Resource,reqparse
app=Flask(__name__)
api=Api(app)
# 欢迎界面
class Hello_View(Resource):
def get(self):
return {'code':'200','data':'hello! weclome use student api!'}
api.add_resource(Hello_View,'/Hello/',endpoint='Hello')
# 登录
class Login_View(Resource):
def get(self):
return {'code':'200','data':'login_page'}
def post(self):
# 1. 创建RequestParser实例
parser=reqparse.RequestParser()
# 2. 添加验证参数
# 第一个参数:传递的参数的名称;第二个参数(type): 验证参数的函数(可以自定义验证函数);
# 第三个参数help返回提示;第四个参数 required 为必填参数
parser.add_argument('username',type=str,help='username NOT NULL',required=True)
parser.add_argument('password',type=str,help='password NOT NULL',required=True)
#return jsonify(parser.parse_args()) # 返回获取到的传递参数
args = parser.parse_args()
if args['username'] != 'admin':
return {'code':'200','data':'username is error'}
elif args['password'] != '123':
return {'code':'200','data':'password is error'}
elif args['username'] == 'admin' and args['password'] == '123':
return {'code':'200','data':'login_succeed','token':'ASd2asdAsdfq'}
api.add_resource(Login_View,'/Login/',endpoint='login')
# 查看所有学生信息
student_list = [
{
'id':'001',
'sex':'1',
'name':'xiaoming'
},
{
'id':'002',
'sex':'0',
'name':'xiaohong'
},
{
'id':'003',
'sex':'1',
'name':'xiaoqiang'
}
]
@app.route('/student/',methods=['GET'])
def all_student():
return jsonify({'student':student_list})
# 添加学生信息
class insert_student_View(Resource):
def post(self):
parser=reqparse.RequestParser()
parser.add_argument('id',type=str,help='id NOT NULL',required=True)
parser.add_argument('sex',type=str,help='sex NOT NULL',required=True)
parser.add_argument('name',type=str,help='name NOT NULL',required=True)
args = parser.parse_args()
student = {
'id':args['id'],
'sex':args['sex'],
'name':args['name'],
}
# 把获取的内容放入新建的字典
student_update={}
student_update['id']=student['id']
student_update['sex']=student['sex']
student_update['name']=student['name']
# 获取student_list的id
list_2 = []
for i in student_list:
list_2.append(i['id'])
# student_list的id 和student_update的id比较
if student_update['id'] in list_2:
return {'code':'200','data':'ID exist ,insert fail'}
else:
student_list.append(student_update.copy())
return {'code':'200','text':'insert success!','data':student_update}
api.add_resource(insert_student_View,'/insert_student/',endpoint='insert_student')
# 删除学生信息
class delete_student_View(Resource):
def post(self):
parser=reqparse.RequestParser()
parser.add_argument('id',type=str,help='id NOT NULL',required=True)
args = parser.parse_args()
# 创建student list替换成传递参数,添加到student_list列表
student = {
'id':args['id'],
}
# 创建list,把需要删除的id添加至student_delete
student_delete=[]
student_delete.append(student['id'])
# for 计算num次数
num = 0
for i in student_list:
num = num + 1
# student_list 的id 和 student_delete 获取的第一给元素匹配,如果等于就删除
if i['id'] == student_delete[0]:
del student_list[num-1]
return {'code':'200','text':'delete success!','data':student_delete}
api.add_resource(delete_student_View,'/delete_student/',endpoint='delete_student')
# 修改学生信息
class update_student_View(Resource):
def post(self):
parser=reqparse.RequestParser()
parser.add_argument('id',type=str,help='id NOT NULL',required=True)
parser.add_argument('sex',type=str,help='sex NOT NULL',required=True)
parser.add_argument('name',type=str,help='name NOT NULL',required=True)
args = parser.parse_args()
id = args['id']
sex = args['sex']
name = args['name']
sum = 0
for i in student_list:
sum = sum + 1
if i['id'] in id:
print('ok')
print(sum)
student_list[sum-1]['id']=id
student_list[sum-1]['sex']=sex
student_list[sum-1]['name']=name
return {'code':'200','data':'update success!','id':id,'sex':sex,'name':name}
api.add_resource(update_student_View,'/update_student/',endpoint='update_student')
if __name__ == '__main__':
app.run(debug=True)
资源已上传
https://download.csdn.net/download/qq_26086231/23561000
|