flask_restful蓝图使用方法

flask-restful蓝图

导入Api方法

1
2
3
4
5
6
7
8
9
10
11
12
13
from flask import Flask, Blueprint
from flask_restful import Api, Resource, url_for

app = Flask(__name__)
api_bp = Blueprint('api', __name__)
api = Api(api_bp)

class TodoItem(Resource):
def get(self, id):
return {'task': 'Say "Hello, World!"'}

api.add_resource(TodoItem, '/todos/<int:id>')
app.register_blueprint(api_bp)

建议新建不同功能的api目录文件

1
2
3
4
5
6
7
from flask import Flask, Blueprint
from flask_restful import Api, Resource

api_bp = Blueprint('api', __name__)
api = Api(api_bp)

import app.api_bp.views,test

然后在app的init中注册
app.register_blueprint(api_bp, url_prefix='/api')
这里通过url——prefix给api区分不同的路径

小伟科技 wechat
本人微信!欢迎关注我!
欢迎打赏支持!