Flask_RESTful返回标准化参数:
from flask import Flask
from flask_restful import Api,Resource,fields,marshal_with
app = Flask(__name__)
api = Api(app)
#flask_restful返回标准化参数
class News(object):
def __init__(self,title,content):
self.title =title
self.content =content
news = News('能力强的体现','能屈能伸')
class NewsView(Resource):
resource_fields ={
'title': fields.String,
'content':fields.String
}
@marshal_with(resource_fields)
def get(self):
# restful规范中,要求,定义好了返回的参数个数 和 内容
# return {'title':"世界太大",'content':"可钱包太小"}
#好处1:体现规范化,即使content这个参数没有值,也应该返回,返回一个null回去
# return {'title':"世界太大"}
#好处2:体现规范化,还可以返回一个对象模型回去
return news
api.add_resource(NewsView,'/news/')
if __name__ == '__main__':
app.run(debug=True)