zoukankan      html  css  js  c++  java
  • FlaskRESTful之入门

    安装模块

    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()
    该花的钱要花,该吃的饭要吃。
  • 相关阅读:
    你用过宏##粘贴函数,然后用函数指针查找执行吗?今天就给你说道说道
    c++nullptr(空指针常量)、constexpr(常量表达式)
    c++bind函数使用
    WordPress高亮代码插件enlighter自定义CSS
    终于有人把云计算、大数据和人工智能讲明白了!
    14. vue源码入口+项目结构分析
    13. Vue CLI脚手架
    12. Vue搭建本地服务
    11. webpack配置Vue
    10. vue之webpack打包原理和用法详解
  • 原文地址:https://www.cnblogs.com/chao666/p/12403229.html
Copyright © 2011-2022 走看看