首页 > 其他 > 详细

FlaskRESTful之入门

时间:2020-03-03 17:07:15      阅读:73      评论:0      收藏:0      [点我收藏+]

安装模块

pip install flask_restful

入门使用

使用步骤:

1. 导包

# 1. 导包
from flask_restful import Api, Resource

2. 创建api对象并接管app

# 2. 创建API对象并接管app
api = Api(app)

3. 创建类视图

# 3. 定义类视图
class IndexResource(Resource):
    def get(self):
        return index

4. 使用api对象给类视图添加路由

# 4. 使用api对象添加路由
api.add_resource(IndexResource, /)

案例代码:

from flask import Flask
# 1. 导包
from flask_restful import Api, Resource

app = Flask(__name__)

# 2. 创建API对象并接管app
api = Api(app)


# 3. 定义类视图
class IndexResource(Resource):
    def get(self):
        return index

# 4. 使用api对象添加路由
api.add_resource(IndexResource, /)

if __name__ == __main__:
    app.run()

结合蓝图使用

使用步骤:

1. 导包

# 1. 导包
from flask_restful import Api, Resource
from flask.blueprints import Blueprint

2. 创建蓝图对象

3. 创建api对象并接管蓝图

# 2. 创建蓝图对象,并使用API对象接管蓝图
bule_prin = Blueprint(flask_blueprint, __name__)
api = Api(bule_prin)

4. 定义类视图

5. 使用api对象给类视图添加路由

# 3. 定义试图,并使用API对象添加路由
class IndexResource(Resource):
    def get(self):
        return index
api.add_resource(IndexResource, /)

6. 注册蓝图

# 4.将蓝图注册到app中
app.register_blueprint(bule_prin)

案例代码:

from flask import Flask
# 1. 导包
from flask_restful import Api, Resource
from flask.blueprints import Blueprint

app = Flask(__name__)

# 2. 创建蓝图对象,并使用API对象接管蓝图
bule_prin = Blueprint(flask_blueprint, __name__)
api = Api(bule_prin)


# 3. 定义试图,并使用API对象添加路由
class IndexResource(Resource):
    def get(self):
        return index
api.add_resource(IndexResource, /)

# 4.将蓝图注册到app中
app.register_blueprint(bule_prin)

if __name__ == __main__:
    app.run()

结合装饰器使用

使用步骤:

1. 导包

from flask_restful import Api, Resource

2. 创建api对象并接管app

api = Api(app)

3. 编写装饰器

# 2. 定义装饰器
def outter(func):
    def inner(*args, **kwargs):
        ret = func(*args, **kwargs)
        return {} decorators.....format(ret)
    return inner

4. 定义类视图指定请求方法的装饰器,并使用api给类视图添加路由

# 1. 定义类视图,并设置路由
class IndexResource(Resource):
    # 为所有请求方法都添加装饰器
    # method_decorators = [outter]

    # 为指定方法添加装饰器
    method_decorators = {
        get: [outter]
    }

    def get(self):
        return get ...

    def post(self):
        return post ...

api.add_resource(IndexResource, /)

装饰器的指定方式:

1. 给所有请求方法都添加装饰器

# 为所有请求方法都添加装饰器
    method_decorators = [outter]

2. 给指定的请求方法添加装饰器

    # 为指定方法添加装饰器
    method_decorators = {
        get: [outter]
    }

案例代码:

from flask import Flask
from flask_restful import Api, Resource
app = Flask(__name__)
api = Api(app)

# 2. 定义装饰器
def outter(func):
    def inner(*args, **kwargs):
        ret = func(*args, **kwargs)
        return {} decorators.....format(ret)
    return inner


# 1. 定义类视图,并设置路由
class IndexResource(Resource):
    # 为所有请求方法都添加装饰器
    # method_decorators = [outter]

    # 为指定方法添加装饰器
    method_decorators = {
        get: [outter]
    }

    def get(self):
        return get ...

    def post(self):
        return post ...

api.add_resource(IndexResource, /)

if __name__ == __main__:
    app.run()

FlaskRESTful之入门

原文:https://www.cnblogs.com/chao666/p/12403229.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!