Flask Restful
video 99 API规范
1 from flask_restful import Api,Resource 2 3 app = Flask(__name__) 4 api = Api(app) 5 6 7 @app.route('/') 8 def hello_world(): 9 return 'Hello World!' 10 11 class LoginView(Resource): 12 def post(self): 13 return {"username":"zhiliao"} 14 api.add_resource(LoginView,'/login/', endpoint="login")
video 101
检查表单:
先引入“from flask_restful import Api,Resource,reqparse”
1 class LoginView(Resource): 2 def post(self): 3 parser = reqparse.RequestParser() 4 parser.add_argument('user', type=int,help='用户名错误') 5 args = parser.parse_args() 6 print(args) 7 8 return {"username":"zhiliao"} 9 api.add_resource(LoginView,'/login/', endpoint="login")
然后用postman去模拟:
因为我们把user设置为int型,所以提示了help中的内容。
video 102 flask-restful标准化返回参数
1,最简单的API写法:
1 from flask_restful import Api,Resource 2 app = Flask(__name__) 3 api = Api(app) 4 5 @app.route('/') 6 def hello_world(): 7 return 'Hello World!' 8 9 class ArticleView(Resource): 10 def get(self): 11 return {"article":"xxx","title":"aaa"} 12 api.add_resource(ArticleView,'/article/',endpoint = 'article')
2,返回值定义
1 from flask_restful import Api,Resource,fields,marshal_with 2 app = Flask(__name__) 3 api = Api(app) 4 5 @app.route('/') 6 def hello_world(): 7 return 'Hello World!' 8 9 class ArticleView(Resource): 10 resource_fields = { 11 'article':fields.String, 12 'title':fields.String, 13 } 14 #marshal_with作用:连接resource_fields到视图。 15 #因为在resource_fields中定义好了返回的参数,即使这个参数没有值,也会返回,但返回一个None 16 #以上写法才是标准API 17 @marshal_with(resource_fields) 18 def get(self): 19 return {"article":"xxx","title":"aaa"} 20 api.add_resource(ArticleView,'/article/',endpoint = 'article')
以上是返回字典,下面讲返回模型:
3,返回模型
1 from flask import Flask 2 from flask_restful import Api,Resource,fields,marshal_with 3 app = Flask(__name__) 4 api = Api(app) 5 6 @app.route('/') 7 def hello_world(): 8 return 'Hello World!' 9 10 #建立模型 11 class Article(object): 12 def __init__(self,article,title): 13 self.article = article 14 self.title = title 15 article = Article(article='book1',title='chapter2') 16 17 class ArticleView(Resource): 18 resource_fields = { 19 'article':fields.String, 20 'title':fields.String, 21 } 22 #marshal_with作用:连接resource_fields到视图。 23 #因为在resource_fields中定义好了返回的参数,即使这个参数没有值,也会返回,但返回一个None 24 #以上写法才是标准API 25 @marshal_with(resource_fields) 26 def get(self): 27 # return {"article":"xxx","title":"aaa"} 28 #Note:这里返回实例化对象就行。 29 return article 30 api.add_resource(ArticleView,'/article/',endpoint = 'article')
29这里直接返回一个实例化后的模型就行。效果和之前一样:
video 103 标准化用法(开发过程中)
video 104 渲染模板
1 from flask_restful import Api,Resource 2 app = Flask(__name__) 3 api = Api(app) 4 5 class ListView(Resource): 6 def get(self): 7 return render_template("index.html") 8 api.add_resource(ListView,'/list/',endpoint='list')
在restful下渲染html。
结果发现:
那么,如何解决这个问题呢?
因为restful返回的时候都是倒数第二个json形式,所以出现上面的形式,参考下表:
所以我们需要修改他为html代码,就应该改为第一种html形式。
1 from flask import Flask,render_template,make_response 2 from flask_restful import Api,Resource 3 app = Flask(__name__) 4 api = Api(app) 5 6 #修改返回的类型 7 @api.representation('text/html') 8 def output_html(data,code,headers): 9 print(data) 10 #在representation装饰的函数中必须返回一个Response对象。 11 # resp = make_response('hello') 12 resp = make_response(data) 13 return resp 14 15 class ListView(Resource): 16 def get(self): 17 return render_template("index.html") 18 api.add_resource(ListView,'/list/',endpoint='list')