Restful
djano restful(是一种api接口的设计规范,通常路由的编写不会出现动词)
class userinfo:
def get:获取数据
def post:添加数据
def put : 修改数据
def delete:删除数据
flask中restful
插件:flask-restful
安装插件
pip install flask-restful
restful 的使用
main.py restful 配置信息
视图的使用
from main import api
from flask_restful import Resource
@api.resource("/Api/v1/leave/")
class LeaveApi(Resource):
def get(self):
"""
处理get请求
:return:
"""
data = request.args
return "get 请求 %s" % data
def post(self):
"""
处理post请求
:return:
"""
data = request.form
return "post 请求 %s" % data
def put(self):
"""
处理put请求
:return:
"""
data = request.form
return "put 请求 %s" % data
def delete(self):
"""
处理delete方法
:return:
"""
data = request.form
return "delete 请求 %s" % data
get请求
初步封装
@api.resource("/Api/v1/leave/")
class LeaveApi(Resource):
def get(self):
"""
处理get请求
获取资源
:return:
"""
data = request.args
id = data.get("id")
result_data = {}
if id:
leave = Leave.query.get(int(id))
if leave is not None:
print (leave)
result_data = {
"request_id":leave.request_id,
"request_name":leave.request_name,
"request_type":leave.request_type,
"request_start":str(leave.request_start),
"request_end":str(leave.request_end),
"request_description":leave.request_description,
"request_phone":leave.request_phone,
"request_status":leave.request_status
}
else:
leaves = Leave.query.all() ### 所有数据
result_data = []
for leave in leaves:
info = {
"request_id": leave.request_id,
"request_name": leave.request_name,
"request_type": leave.request_type,
"request_start": str(leave.request_start),
"request_end": str(leave.request_end),
"request_description": leave.request_description,
"request_phone": leave.request_phone,
"request_status": leave.request_status
}
result_data.append(info)
return result_data
# return "get 请求 %s" % data
def post(self):
"""
处理post请求
:return:
"""
data = request.form
return "post 请求 %s" % data
def put(self):
"""
处理put请求
:return:
"""
data = request.form
return "put 请求 %s" % data
def delete(self):
"""
处理delete方法
:return:
"""
data = request.form
return "delete 请求 %s" % data
提取封装公共的返回结构
get请求中代码冗余,将公共部分提取封装
get请求封装:
@api.resource("/Api/v1/leave/")
class LeaveApi(Resource):
def __init__(self):
super(LeaveApi, self).__init__()
self.result = {
"method": "get",
"version": "v1",
"data":""
}
def create_data(self,leave):
"""
定义返回的数据
:return:
"""
result_data = {
"request_id": leave.request_id,
"request_name": leave.request_name,
"request_type": leave.request_type,
"request_start": str(leave.request_start),
"request_end": str(leave.request_end),
"request_description": leave.request_description,
"request_phone": leave.request_phone,
"request_status": leave.request_status
}
return result_data
def get(self):
"""
处理get请求
获取资源
:return:
"""
data = request.args
id = data.get("id")
result_data = {}
if id:
leave = Leave.query.get(int(id))
if leave is not None:
result_data = self.create_data(leave)
else:
leaves = Leave.query.all() ### 所有数据
result_data = []
for leave in leaves:
info = self.create_data(leave)
result_data.append(info)
self.result["data"] = result_data
return jsonify(self.result)
封装删除方法
def delete(self):
"""
处理delete方法 删除数据
:return:
"""
data = request.form
id = data.get("id")
leave = Leave.query.get(id)
leave.delete()
self.result["method"] = "delete"
self.result["msg"] = "删除成功"
return jsonify(self.result)
接口(api)
api接口提供数据的支持,将前端html和后端进行分离
返回的数据是json
restful风格的接口,命名不出现动词
接口开发的目的,增加数据的可用性
(前端,app,pc,小程序,爬虫)
写web页面,请求api接口获取数据,前端web (ajax,vue)
Flask-migrate
migrate是flask对数据模型的管理插件
安装
pip install flask-migrate
migrate 插件通常不单独使用,需要结合flask的script插件进行使用。
能够解决 create_all 不足的问题。
使用
main.py
命令
python manage.py db init
python manage.py db migrate
python manage.py db upgrade
CSRF
flask_wtf 昨天form表单类,csrf_token会随着form表单生成,但是不校验
CSRF校验:
使用:
开启csrf校验
前端页面使用:
避免:
装饰器 给flask中类视图增加装饰器
给函数视图增加装饰器 @func
method_decorators 类属性,Resource会检测method_decorators类属性当中有没有给下面的方法增加装饰器
method_decorators 会给方法增加装饰器
method_decorators = [] 代表将该类中的所有方法都增加装饰器
class Demo(Resource):
method_decorators = [func1,func2]
def get(self):
"""
:return:
"""
return "get请求"
def post(self):
"""
:return:
"""
return "post 请求"
api.add_resource(Demo,"/Demo/") ##
@func2
@func1
def get():
pass
给指定的方法增加指定的装饰器
请求上下文:
-
request 负责请求,获取请求的方式,获取请求的数据
-
session 负责session设置,获取,删除